#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<pair<long long, long long>> k;
  for (int i = 1; i <= n; i++) {
    long long cur;
    cin >> cur;
    k.push_back({cur, i});
  }
  sort(k.begin(), k.end());
  long long ans = 0;
  int left = 1, right = n;
  for (int i = n - 1; i >= 0; i--) {
    if (abs(k[i].second - left) >= abs(k[i].second - right)) {
      ans += k[i].first * (abs(k[i].second - left));
      left++;
    } else {
      ans += k[i].first * (abs(k[i].second - right));
      right--;
    }
  }
  cout << ans << endl;
  return 0;
}
