#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  int T;
  T = 1;
  while (T--) {
    int n, i, j;
    cin >> n;
    vector<pair<long long, long long> > v;
    long long ar[n + 1], flag[n + 1];
    for (i = 1; i <= n; ++i) {
      cin >> ar[i];
      flag[i] = 0;
    }
    long long l = 1, r = n;
    long long ans = 0, ans2, pos;
    while (l <= r) {
      ans2 = 0;
      for (i = 1; i <= n; ++i) {
        if (flag[i] == 0) {
          if (ans2 < ar[i] * abs(i - l)) {
            ans2 = ar[i] * abs(i - l);
            pos = i;
          }
          if (ans2 < ar[i] * abs(i - r)) {
            ans2 = ar[i] * abs(i - r);
            pos = i;
          }
        }
      }
      ans = ans + ans2;
      i = pos;
      flag[pos] = 1;
      if (ans2 == ar[i] * abs(i - l)) {
        ++l;
      } else {
        --r;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
