#![allow(non_snake_case)] #[allow(unused_imports)] use proconio::{fastout, input, marker::*}; #[fastout] fn main() { input! { N: usize, A: [usize; N], } let mut ans = 0; let mut vp = [0; 501]; let mut v = [0; 501]; let mut p = [0; 501]; let mut index = [0; 501]; for i in (0..N).rev() { let a = A[i]; for j in 0..=500 { if a >= j { ans += a * p[j]; ans += i * v[j]; ans -= vp[j]; ans -= i * A[i] * index[j]; } else { ans += vp[j]; ans += i * A[i] * index[j]; ans -= a * p[j]; ans -= i * v[j]; } } vp[a] += a * i; v[a] += a; p[a] += i; index[a] += 1; } println!("{}", ans * 2); }