#include <bits/stdc++.h>
using namespace std;
const int modn = 1e9 + 7;
int mod(int x) { return x < 0 ? x + modn : x < modn ? x : x - modn; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  long long ans = 0;
  priority_queue<pair<int, int> > que;
  for (int i = 1; i <= n; ++i) {
    que.push({a[i], i});
  }
  int l = 1, r = n;
  while (!que.empty()) {
    auto top = que.top();
    que.pop();
    int val = top.first, pos = top.second;
    if (abs(pos - l) > abs(pos - r)) {
      ans = ans + 1ll * val * abs(pos - l);
      l++;
    } else {
      ans = ans + 1ll * val * abs(pos - r);
      r--;
    }
  }
  cout << ans << "\n";
  return 0;
}
