#include <bits/stdc++.h>
using namespace std;
signed main() {
  std::ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  vector<pair<long long, long long>> A(n);
  for (long long i = 0; i < n; i++) {
    cin >> A[i].first;
    A[i].second = i;
  }
  sort((A).begin(), (A).end());
  reverse((A).begin(), (A).end());
  long long l = 0, r = n - 1;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    if (A[i].second >= l + (r - l + 1) / 2) {
      sum += A[i].first * (abs(A[i].second - l));
      l++;
    } else {
      sum += A[i].first * (abs(A[i].second - r));
      r--;
    }
  }
  cout << sum;
}
