有n个数,分别为a 1, a 2, a 3, a 4, …, a n
设 ans(x,y)=i=x∑y−1j=i+1∑yai∗aj
sum(x)=i=1∑xai
cost(y)=ans(1,y)=i=1∑y−1j=i+1∑yai∗aj
结论:ans(x,y)=cost[y]−cost[x−1]−sum[x−1]∗(sum[y]−sum[x−1]))
for(int i = 1; i <= n; i++){
scanf("%lld", &val[i]);
sum[i] = sum[i - 1] + val[i];
}
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
cost[j] += val[i] * val[j];
}
}
for(int i = 1; i <= n; i++){
cost[i] += cost[i - 1];
}