#include <bits/stdc++.h>
using namespace std;
int N, ans;
int main() {
  cin >> N;
  vector<int> A(N), B(N);
  bool used[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    used[i] = false;
  }
  B = A;
  sort(B.begin(), B.end(), std::greater<int>());
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (A[j] == B[i]) {
        int l = 0, r = N - 1;
        while (used[l]) l++;
        while (used[r]) r--;
        if (abs(j - l) > abs(j - r)) {
          used[l] = true, ans += B[i] * abs(j - l);
        } else {
          used[r] = true, ans += B[i] * abs(j - r);
        }
        if (i + 1 < N && B[i] == B[i + 1]) {
          i++;
          continue;
        } else
          break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
