#include <bits/stdc++.h>
using namespace std;
long long N;
long long A[200001];
int main() {
  vector<pair<long long, long long> > B;
  cin >> N;
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
    B.push_back({A[i], i});
  }
  sort(B.rbegin(), B.rend());
  long long l = 0, r = N - 1, m = (N - 1) / 2;
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    pair<long long, long long> p = B[i];
    if (B[i].second < m) {
      ans += B[i].first * abs(r - B[i].second);
      r--;
      m = (l + r) / 2;
    } else {
      ans += B[i].first * abs(l - B[i].second);
      l++;
      m = (l + r) / 2;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
