#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<pair<long, int> > p(n);
  for (int i = 0; i < n; i++) {
    p[i].first = a[i];
    p[i].second = i;
  }
  sort(p.begin(), p.end(), greater<pair<long, int> >());
  int left = 0;
  int right = n - 1;
  long ans = 0;
  for (int i = 0; i < n; i++) {
    int beforePlace = p[i].second;
    long performance = p[i].first;
    int chk = 1;
    if (abs(beforePlace - left) > abs(beforePlace - right))
      chk = 1;
    else if (abs(beforePlace - left) < abs(beforePlace - right))
      chk = 2;
    else {
      if (i == n - 1)
        chk = 1;
      else {
        for (int j = 1; i + j < n; j++) {
          if (abs(p[i + j].second - left) > abs(p[i + j].second - right)) {
            chk = 2;
            break;
          } else if (abs(p[i + j].second - left) <
                     abs(p[i + j].second - right)) {
            chk = 1;
            break;
          }
        }
      }
    }
    if (chk == 1) {
      ans += performance * abs(beforePlace - left);
      left++;
    } else {
      ans += performance * abs(beforePlace - right);
      right--;
    }
  }
  cout << ans << endl;
}
