#include <bits/stdc++.h>
using namespace std;
const long long int LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < (N); ++i) {
    cin >> A[i];
  }
  priority_queue<pair<int, int> > que;
  for (int i = 0; i < (N); ++i) {
    que.push(pair<int, int>(A[i], i));
  }
  int l = 0, r = N - 1;
  long long int ans = 0;
  while (!que.empty()) {
    int i = que.top().second;
    int a = que.top().first;
    que.pop();
    if (abs(i - l) > abs(r - i)) {
      ans += a * abs(i - l);
      l++;
    } else {
      ans += a * abs(r - i);
      r--;
    }
  }
  cout << ans << endl;
  return 0;
}
