#include <bits/stdc++.h>
using namespace std;
using P = pair<int64_t, int64_t>;
int main() {
  int N;
  cin >> N;
  vector<int64_t> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec[i];
  }
  priority_queue<P> mae;
  priority_queue<P> ushiro;
  for (int i = 0; i < N; i++) {
    P p = make_pair(vec[i] * i, i);
    mae.push(p);
    p = make_pair(vec[i] * (N - 1 - i), i);
    ushiro.push(p);
  }
  int64_t ans = 0;
  int mae_cnt = 0;
  int ushiro_cnt = 0;
  set<int64_t> S;
  while (mae_cnt + ushiro_cnt < N) {
    P m = mae.top();
    P u = ushiro.top();
    int64_t M = m.first - mae_cnt * vec[m.second];
    int64_t U = u.first - ushiro_cnt * vec[u.second];
    if (M >= U) {
      ans += M;
      mae_cnt++;
      S.insert(m.second);
    } else {
      ans += U;
      ushiro_cnt++;
      S.insert(u.second);
    }
    while (mae.size()) {
      if (S.count(mae.top().second)) {
        mae.pop();
      } else {
        break;
      }
    }
    while (ushiro.size()) {
      if (S.count(ushiro.top().second)) {
        ushiro.pop();
      } else {
        break;
      }
    }
  }
  cout << ans << endl;
}
