#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  multiset<pair<long long, long long> > s;
  long long n;
  cin >> n;
  long long a;
  long long b[n];
  for (int i = 0; i < n; i++) {
    cin >> a;
    b[i] = a;
    s.insert({a, i});
  }
  long long ans = 0;
  long long ma = n - 1, mi = 0;
  long long num;
  while (!s.empty()) {
    auto itr = s.begin();
    num = 0;
    for (auto& i : s) {
      if (i.first * abs(i.second - ma) > num) {
        itr = s.find(i);
        num = i.first * abs(i.second - ma);
      }
      if (i.first * abs(i.second - mi) > num) {
        itr = s.find(i);
        num = i.first * abs(i.second - mi);
      }
    }
    long long maki = itr->first, niko = itr->second;
    s.erase(itr);
    if (num == maki * abs(niko - ma)) {
      ans += num;
      ma--;
    } else {
      ans += num;
      mi++;
    }
  }
  ma = n - 1, mi = 0;
  multiset<pair<long long, long long> > ms;
  for (int i = 0; i < n; i++) {
    ms.insert({b[i], i});
  }
  num = 0;
  for (auto itr = ms.end(); itr != ms.begin();) {
    itr--;
    if (abs(ma - itr->second) > abs(mi - itr->first)) {
      num += itr->first * abs(ma - itr->second);
      ma--;
    } else {
      num += itr->first * abs(mi - itr->second);
      mi++;
    }
  }
  ans = max(ans, num);
  cout << ans << endl;
  return 0;
}
