#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  set<pii> left, right;
  for (int i = 1; i <= n; ++i) {
    right.emplace(-a[i], i);
  }
  ll ans = 0;
  for (int cnt = 1; cnt < n; ++cnt) {
    left.emplace(-a[cnt], cnt);
    right.erase(make_pair(-a[cnt], cnt));
    ll cur = 0;
    int pos = n;
    for (auto [v, i] : left) {
      cur += 1ll * abs(pos-- - i) * -v;
    }
    pos = 1;
    for (auto [v, i] : right) {
      cur += 1ll * abs(pos++ - i) * -v;
    }
    ans = max(ans, cur);
  }
  cout << ans << endl;
}
