#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
int cansel_sync = (ios::sync_with_stdio(0), cin.tie(0), 0);
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 100010;
const double err = 1e-11;
const int inf = ~0u >> 2;
const long long INF = ~0ull >> 2;
long long read() {
  long long x;
  if (scanf("%lld", &x) == -1) exit(0);
  return x;
}
const double pi = acos(-1.0);
double readf() {
  double x;
  if (scanf("%lf", &x) == -1) exit(0);
  return x;
}
template <typename T>
T sqr(const T &x) {
  return x * x;
}
template <typename A, typename B>
ostream &operator<<(ostream &o, const pair<A, B> &x) {
  return o << '(' << x.first << ',' << x.second << ')';
}
int mod = (1 ? 1000000007 : 998244353);
long long mul(long long a, long long b, long long m = mod) { return a * b % m; }
long long qpow(long long a, long long b, long long m = mod) {
  long long ans = 1;
  for (; b; a = mul(a, a, m), b >>= 1)
    if (b & 1) ans = mul(ans, a, m);
  return ans;
}
long long getinv(long long v, long long m = mod) { return qpow(v, m - 2, m); }
long long a[N], to[N];
long long now = 0, ans = 0;
void test(long long x, long long y) {
  long long now2 = now - abs(x - to[x]) * a[x] - abs(y - to[y]) * a[y] +
                   abs(x - to[y]) * a[x] + abs(y - to[x]) * a[y];
  if (now2 > now || (now2 > 0.9 * now && rnd() % 2)) {
    now = now2;
    ans = max(ans, now);
    swap(to[x], to[y]);
  }
}
long long n;
signed main() {
  cin >> n;
  for (long long i = (0), _ = (n); i < _; i++) cin >> a[i], to[i] = i;
  while (clock() < 1.9 * CLOCKS_PER_SEC) {
    long long k = rnd() % n;
    for (long long i = (0), _ = (n); i < _; i++) {
      test(i, k);
    }
  }
  cout << ans << endl;
  return 0;
}
