#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long N;
  cin >> N;
  long long ans = 0;
  map<long long, long long> memo;
  for (long long i = (0); i < (N); ++i) {
    long long a;
    cin >> a;
    memo.insert(make_pair(-a, i));
  }
  vector<bool> check(N, true);
  for (auto it = memo.begin(); it != memo.end(); it++) {
    long long d = it->second;
    long long rs = N - 1 - d;
    long long ls = d;
    for (long long i = max(rs, ls); i >= 0; i--) {
      if (i <= ls && check[d - i]) {
        ans += it->first * i * (-1);
        check[d - i] = false;
        break;
      }
      if (i <= rs && check[d + i]) {
        ans += it->first * i * (-1);
        check[d + i] = false;
        break;
      }
    }
  }
  cout << ans << endl;
}
