#include <bits/stdc++.h>
using namespace std;
const long long LINF = 1e18L + 1;
const int INF = 1e9 + 1;
int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < (N); ++i) cin >> A[i];
  vector<long long> ord(A);
  sort(ord.begin(), ord.end(), [](long long a, long long b) { return a > b; });
  long long left = 0, right = N - 1;
  long long ans = 0;
  for (int i = 0; i < (ord.size()); ++i) {
    bool pending = false;
    for (int j = 0; j < (N); ++j) {
      if (A[j] == ord[i]) {
        if ((j - left) == (right - j)) {
          ans += (j - left) * A[j];
          pending = true;
          continue;
        }
        if ((j - left) > (right - j)) {
          if (pending) right--;
          ans += (j - left) * A[j];
          left++;
        } else {
          if (pending) left++;
          ans += (right - j) * A[j];
          right--;
        }
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
