#include <bits/stdc++.h>
using namespace std;
int N;
vector<long long> A;
struct infant {
  int i;
  long long v;
};
int main(void) {
  cin >> N;
  A.assign(N, 0);
  vector<infant> AR(N);
  vector<infant> AL(N);
  for (int i = (0); i < (int)(N); ++i) {
    cin >> A[i];
    AL[i].i = i;
    AL[i].v = A[i] * i;
    AR[i].i = i;
    AR[i].v = A[i] * (N - i - 1);
  }
  sort((AL).begin(), (AL).end(),
       [](const infant& lh, const infant& rh) { return lh.v > rh.v; });
  sort((AR).begin(), (AR).end(),
       [](const infant& lh, const infant& rh) { return lh.v > rh.v; });
  long long ans = 0;
  long long li = 0, lc = 0;
  long long ri = 0, rc = 0;
  set<int> used;
  for (int i = 0; i < N; ++i) {
    infant lmax, rmax;
    for (int k = li; k < N; ++k) {
      if (used.find(AL[k].i) == used.end()) {
        lmax = AL[k];
        li = k;
        break;
      }
    }
    for (int k = ri; k < N; ++k) {
      if (used.find(AR[k].i) == used.end()) {
        rmax = AR[k];
        ri = k;
        break;
      }
    }
    long long lv = lmax.v - (lc * A[lmax.i]);
    long long rv = rmax.v - (rc * A[rmax.i]);
    if (lv > rv || (rv == lv && A[lmax.i] >= A[rmax.i])) {
      ans += lv;
      lc++;
      li++;
      used.insert(lmax.i);
    } else {
      ans += rv;
      rc++;
      ri++;
      used.insert(rmax.i);
    }
  }
  cout << ans << endl;
  return 0;
}
