#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
  vector<pair<long long, long long> > a;
  long long n, ans = 0, ans2 = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long b;
    cin >> b;
    a.push_back(pair<long long, long long>(b, i));
  }
  sort((a).rbegin(), (a).rend());
  bool f = 0;
  long long l2 = 0, r2 = n - 1, ho = 0, h = 0;
  char q2 = 0;
  for (int j = 0; j < n; j++) {
    char q;
    long long l = l2, r = r2, ne = 0;
    for (int i = j; i < n; i++) {
      if (abs(l - a[i].second) == abs(r - a[i].second)) {
        if (f) {
          ans += ne;
          ans += abs(l - a[i].second) * a[i].first;
          l++;
          r--;
          f = 0;
          ne = 0;
          if (j + 1 == i) {
            q = 'r';
          }
        } else {
          ne = abs(l - a[i].second) * a[i].first;
          f = 1;
        }
      } else if (abs(l - a[i].second) > abs(r - a[i].second)) {
        if (f) {
          ans += ne;
          ans += abs(l - a[i].second) * a[i].first;
          l++;
          r--;
          f = 0;
          ne = 0;
          if (j + 1 == i) {
            q = 'r';
          }
        } else {
          ans += abs(l - a[i].second) * a[i].first;
          l++;
          if (j + 1 == i) {
            q = 'l';
          }
        }
      } else {
        if (f) {
          ans += ne;
          ans += abs(r - a[i].second) * a[i].first;
          l++;
          r--;
          f = 0;
          ne = 0;
          if (j + 1 == i) {
            q = 'l';
          }
        } else {
          ans += abs(r - a[i].second) * a[i].first;
          r--;
          if (j == i) {
            q = 'r';
          }
        }
      }
    }
    if (j != 0) {
      if (ans2 < ans + ne) {
        ans2 = ans + ne;
        ho += min(abs(l2 - a[j - 1].second), abs(r2 - a[j - 1].second)) *
              a[j - 1].first;
      } else {
        ho += max(abs(l2 - a[j - 1].second), abs(r2 - a[j - 1].second)) *
              a[j - 1].first;
        if (q2 == 'r') {
          r2--;
          l2--;
        } else {
          r2++;
          l2++;
        }
      }
    } else {
      ans2 = ans + ne;
    }
    h = min(abs(l2 - a[j].second), abs(r2 - a[j].second)) * a[j].first;
    if (q == 'r') {
      q2 = q;
      l2++;
    } else {
      q2 = q;
      r2--;
    }
    ans = ho + h;
    ne = 0;
    f = 0;
  }
  cout << ans2 << "\n";
}
