https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_bh
これも数え方の問題ですね。の係数は1がi-1個、-1が(N-i)個なので、合わせて重みは(2i-N-1)となります。
// Sum of difference Easy #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } //////////////////// process //////////////////// fn read_input() -> Vec<i64> { let _N: usize = read(); let A = read_vec(); A } fn f(A: Vec<i64>) -> i64 { let N = A.len(); let mut s: i64 = 0; for i in 0..N { s += (1 - (N as i64) + 2 * (i as i64)) * A[i] } s } fn main() { let A = read_input(); println!("{}", f(A)) }