#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
  int N;
  cin >> N;
  vector<pair<long long, int> > v(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i].first;
    v[i].second = i;
  }
  int minPos = 0, maxPos = N - 1;
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  long long sum = 0;
  for (auto a : v) {
    long long value = a.first;
    int pos = a.second;
    if (abs(pos - minPos) > abs(maxPos - pos)) {
      sum += value * abs(pos - minPos);
      minPos++;
    } else {
      sum += value * abs(maxPos - pos);
      maxPos--;
    }
  }
  cout << sum;
}
