#include <bits/stdc++.h>
using namespace std;
int MOD = 998244353;
const int mxN = 2e5 + 9;
long long ar[2010];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long sc;
  long long N;
  cin >> N;
  priority_queue<pair<long long, long long> > pq;
  for (long long i = 1; i <= N; i++) {
    cin >> sc;
    pq.push(make_pair(sc, i));
  }
  long long ans = 0;
  long long mostleft = 1;
  long long mostright = N;
  while (!pq.empty()) {
    long long score = pq.top().first;
    long long pos = pq.top().second;
    pq.pop();
    long long a1 = pos - mostleft;
    long long a2 = mostright - pos;
    if (a1 > a2) {
      ans += score * a1;
      ++mostleft;
    } else if (a2 > a1) {
      ans += score * a2;
      --mostright;
    }
  }
  cout << ans;
}
