AcWing 4644. 求和( java实现 )
原题链接
简单
作者:
nighoc
,
2023-03-28 13:39:24
,
所有人可见
,
阅读 96
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
final int N = 200010;
long[] a = new long[N];
long[] s = new long[N];
int n;
long sum = 0;
public Main() throws IOException {
n = Integer.parseInt(in.readLine());
String[] line = in.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(line[i - 1]);
s[i] = s[i - 1] + a[i]; // 求前缀和
}
for (int i = 1; i <= n - 1; i++) {
sum += (a[i] * (s[n] - s[i]));
}
System.out.println(sum);
in.close();
}
public static void main(String[] args) throws Exception {
new Main();
}
}