#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < (int)(N); i++) cin >> A[i];
  vector<pair<int, int>> p(N);
  for (int i = 0; i < (int)(N); i++) p[i] = make_pair(A[i], i);
  sort(p.begin(), p.end());
  long long ans = 0;
  int a = 0, b = N - 1;
  for (int i = N - 1; i >= 0; i--) {
    int x = p[i].second;
    if (abs(x - a) > abs(x - b)) {
      long long an = p[i].first * abs(x - a);
      ans += an;
      a++;
    } else {
      long long an = p[i].first * abs(x - b);
      ans += an;
      b--;
    }
  }
  cout << ans << endl;
}
