#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, N;
  cin >> N;
  vector<pair<int, int>> A(N);
  vector<bool> occupy(N, false);
  for (i = 0; i < N; i++) {
    cin >> A.at(i).first;
    A.at(i).second = i;
  }
  sort(A.begin(), A.end());
  int num, maxnum;
  long long ans = 0, tmp, maxdis;
  for (i = N - 1; i >= 0; i--) {
    num = A.at(i).second;
    maxdis = 0;
    for (j = 0; j < N; j++) {
      if (occupy.at(j)) continue;
      if (abs(j - num) > maxdis) {
        maxdis = abs(j - num);
        maxnum = j;
      }
    }
    occupy.at(maxnum) = true;
    ans += maxdis * A.at(i).first;
  }
  cout << ans;
  return 0;
}
