#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long MOD = 1e9 + 7;
int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    A[i] = a;
  }
  long long ans = 0;
  long long left = 0;
  long long right = N - 1;
  vector<long long> dbg(N);
  while (left <= right) {
    long long rval = -1;
    long long lval = -1;
    long long lidx = -1;
    long long ridx = -1;
    for (int i = 0; i < N; i++) {
      long long a = A[i];
      if (a == -10000) continue;
      long long vl = a * max(abs(i - left) - 1, 0LL);
      long long vr = a * max(abs(i - right) - 1, 0LL);
      if (vl > lval) {
        lval = vl;
        lidx = i;
      }
      if (vr > rval) {
        rval = vr;
        ridx = i;
      }
    }
    if (lval >= rval) {
      ans += A[lidx] * abs(left - lidx);
      dbg[left] = A[lidx];
      A[lidx] = -10000;
      left++;
    } else {
      ans += A[ridx] * abs(right - ridx);
      dbg[right] = A[ridx];
      A[ridx] = -10000;
      right--;
    }
  }
  cout << ans << endl;
  return 0;
}
