#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
int main() {
  int n;
  cin >> n;
  priority_queue<P> q;
  for (int i = 0; i < (n); i++) {
    int a;
    cin >> a;
    q.push(make_pair(a, i));
  }
  int left = 0;
  int right = n - 1;
  ll ans = 0;
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (abs(left - p.second) >= abs(right - p.second)) {
      ans += p.first * abs(left - p.second);
      left++;
    } else {
      ans += p.first * abs(right - p.second);
      right--;
    }
  }
  cout << ans << endl;
}
