#include using namespace std; #ifdef LOCAL #include "settings/debug.cpp" #else #define Debug(...) void(0) #endif #define rep(i, n) for (int i = 0; i < (n); ++i) using ll = long long; using ull = unsigned long long; int main() { int n; cin >> n; vector a(n); rep(i, n) cin >> a[i]; ll ans = 0; vector cnt(501, 0); vector coef(501, 0); rep(i, n) { for (int j = 1; j <= 500; ++j) coef[j] += cnt[j]; for (int j = 1; j <= 500; ++j) ans += coef[j] * abs(a[i] - j); cnt[a[i]]++; } cout << ans * 2 << endl; return 0; }