#include <bits/stdc++.h>
using namespace std;
long long int arr[200005];
long long int n;
map<pair<long long int, long long int>, long long int> dp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  set<long long int> send;
  for (long long int i = 0; i < n; i++) {
    cin >> arr[i];
    send.insert(i);
  }
  dp[{0, 0}] = 0;
  dp[{0, 1}] = 0;
  set<long long int> sl(send);
  set<long long int> sr(send);
  long long int offsetl = 0, offsetr = 0;
  for (long long int F = 1; F <= n; F++) {
    long long int candill = LONG_LONG_MIN;
    long long int candilr = LONG_LONG_MIN;
    long long int candirl = LONG_LONG_MIN;
    long long int candirr = LONG_LONG_MIN;
    long long int lldex, lrdex, rldex, rrdex;
    long long int wheretoputll = offsetl;
    long long int wheretoputlr = n - (F - offsetl);
    long long int wheretoputrl = offsetr;
    long long int wheretoputrr = n - (F - offsetr);
    for (auto i : sl) {
      if (arr[i] * abs(i - wheretoputll) >= candill) {
        candill = arr[i] * abs(i - wheretoputll);
        lldex = i;
      }
      if (arr[i] * abs(i - wheretoputlr) >= candilr) {
        candilr = arr[i] * abs(i - wheretoputlr);
        lrdex = i;
      }
    }
    for (auto i : sr) {
      if (arr[i] * abs(i - wheretoputrl) >= candirl) {
        candirl = arr[i] * abs(i - wheretoputrl);
        rldex = i;
      }
      if (arr[i] * abs(i - wheretoputrr) >= candirr) {
        candirr = arr[i] * abs(i - wheretoputrr);
        rrdex = i;
      }
    }
    set<long long int> slcopy(sl);
    set<long long int> srcopy(sr);
    long long int offsetlcopy = offsetl, offsetrcopy = offsetr;
    if (candill + dp[{F - 1, 0}] >= dp[{F - 1, 1}] + candirl) {
      sl.erase(lldex);
      dp[{F, 0}] = candill + dp[{F - 1, 0}];
      offsetl = offsetlcopy + 1;
    } else {
      srcopy.erase(rldex);
      sl = srcopy;
      dp[{F, 0}] = dp[{F - 1, 1}] + candirl;
      offsetl = offsetrcopy + 1;
    }
    if (dp[{F - 1, 0}] + candilr >= dp[{F - 1, 1}] + candirr) {
      slcopy.erase(lrdex);
      sr = slcopy;
      dp[{F, 1}] = dp[{F - 1, 0}] + candilr;
      offsetr = offsetlcopy;
    } else {
      sr.erase(rrdex);
      dp[{F, 1}] = dp[{F - 1, 1}] + candirr;
      offsetr = offsetrcopy;
    }
  }
  cout << max(dp[{n, 0}], dp[{n, 1}]) << endl;
}
