#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000000;
const long long mod = 1000000007;
long long test[345678];
map<long long, long long> ok;
signed main() {
  long long n, m, a, b = 0, ans = 0;
  string s, t;
  cin >> n;
  vector<long long> aa, mae, ato;
  long long c = n - 1;
  for (long long h = 0; h < n; h++) {
    cin >> a;
    test[h] = a;
    if (ok[a] == 0) {
      aa.push_back(a);
      ok[a] = 1;
    }
  }
  sort(aa.begin(), aa.end());
  for (long long h = aa.size() - 1; h >= 0; h--) {
    for (long long i = 0; i < n; i++) {
      if (test[i] == aa[h]) {
        if (i >= (n + 1) / 2) {
          mae.push_back(test[i]);
          ans += (i - b) * test[i];
          b++;
        } else {
          ato.push_back(test[i]);
          ans += (c - i) * test[i];
          c--;
        }
      }
    }
  }
  reverse(ato.begin(), ato.end());
  copy(ato.begin(), ato.end(), back_inserter(mae));
  cout << ans << endl;
  return 0;
}
