#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long(i) = 0; (i) < (n); ++(i)) cin >> a[i];
  vector<bool> used(n, false);
  long long l = 0;
  long long r = n - 1;
  vector<long long> ans(n, -1);
  long long ansnum = 0;
  for (long long(i) = 0; (i) < (n); ++(i)) {
    long long num = -1;
    long long pos = -1;
    for (long long(j) = 0; (j) < (n); ++(j)) {
      if (!used[j]) {
        if (num < a[j]) {
          num = a[j];
          pos = j;
        }
      }
    }
    used[pos] = true;
    if (abs(l - pos) > abs(r - pos)) {
      long long ok = ansnum + abs(l - pos) * a[pos];
      long long tmp = ansnum;
      vector<long long> tmpans = ans;
      long long tmpr = r;
      bool aaa = false;
      vector<long long> tttans;
      long long tmppos = l;
      long long zurakazu = 0;
      for (long long j = l - 1; j >= 0; j--) {
        ansnum += a[ans[j]] * (abs(tmpr - ans[j]) - abs(j - ans[j]));
        tmpans[j] = -1;
        tmpans[tmpr] = ans[j];
        if (abs(j - pos) * a[pos] + ansnum >= ok) {
          ok = abs(j - pos) * a[pos] + ansnum;
          aaa = true;
          tttans = tmpans;
          tmppos = j;
          zurakazu = l - 1 - j + 1;
        }
        tmpr--;
      }
      if (aaa) {
        tttans[tmppos] = pos;
        ans = tttans;
        l = tmppos + 1;
        r -= zurakazu;
      } else {
        ans[l] = pos;
        l++;
      }
      ansnum = ok;
    } else {
      long long ok = ansnum + abs(r - pos) * a[pos];
      long long tmp = ansnum;
      vector<long long> tmpans = ans;
      long long tmpl = l;
      bool aaa = false;
      vector<long long> tttans;
      long long tmppos = r;
      long long zurakazu = 0;
      for (long long j = r + 1; j < n; j++) {
        ansnum += a[ans[j]] * (abs(tmpl - ans[j]) - abs(j - ans[j]));
        tmpans[j] = -1;
        tmpans[tmpl] = ans[j];
        if (abs(j - pos) * a[pos] + ansnum >= ok) {
          ok = abs(j - pos) * a[pos] + ansnum;
          aaa = true;
          tttans = tmpans;
          tmppos = j;
          zurakazu = r + 1 - j + 1;
        }
        tmpl--;
      }
      if (aaa) {
        tttans[tmppos] = pos;
        ans = tttans;
        r = tmppos - 1;
        l += zurakazu;
      } else {
        ans[r] = pos;
        r--;
      }
      ansnum = ok;
    }
  }
  cout << ansnum << endl;
}
