#include <bits/stdc++.h>
using namespace std;
class Child {
 public:
  int pos;
  long long active;
  long long maxAct;
};
struct {
  bool operator()(Child a, Child b) const { return a.maxAct > b.maxAct; }
} MyComp;
int main() {
  int N;
  cin >> N;
  vector<Child> A(N);
  for (int i = 0; i < N; ++i) {
    Child tmp;
    cin >> tmp.active;
    tmp.pos = i;
    tmp.maxAct = (tmp.pos < N / 2) ? tmp.active * (N - 1 - tmp.pos)
                                   : tmp.active * (tmp.pos);
    A[i] = tmp;
  }
  sort(A.begin(), A.end(), MyComp);
  int posTop = 0, posBottom = N - 1;
  long long preciousTotal = 0;
  for (int i = 0; i < N; ++i) {
    Child* child = &(A[i]);
    long long preTop = abs(child->active * (child->pos - posTop));
    long long preBottm = abs(child->active * (posBottom - child->pos));
    if (preTop >= preBottm) {
      preciousTotal += preTop;
      posTop += 1;
    } else {
      preciousTotal += preBottm;
      posBottom -= 1;
    }
  }
  cout << preciousTotal;
  return 0;
}
