#include <bits/stdc++.h>
using namespace std;
void resolve() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<pair<int, int>> B(N);
  for (int i = (0); i <= (N - 1); i++) {
    cin >> A[i];
    B[i] = make_pair(A[i], i);
  }
  sort(B.begin(), B.end());
  long long totalL = 0, totalR = 0;
  long long memberL = 0, memberR = 0;
  for (int i = 0; i < B.size(); i++) {
    if (i == 0) {
      if (B[i].second > N / 2) {
        totalL += B[i].first * B[i].second;
        memberL += B[i].first;
      } else {
        totalR += B[i].first * abs(N - 1 - B[i].second);
        memberR += B[i].first;
      }
      continue;
    }
    long long incL = B[i].first * B[i].second - memberL;
    long long incR = B[i].first * abs(N - 1 - B[i].second) - memberR;
    if (incL > incR) {
      totalL += incL;
      memberL += B[i].first;
    } else {
      totalR += incR;
      memberR += B[i].first;
    }
  }
  long long ans = totalL + totalR;
  cout << ans << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  resolve();
  return 0;
}
