#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int n;
  cin >> n;
  vector<pair<long long int, long long int> > vec_pair;
  long long int val, idx;
  for (long long int i = 1; i <= n; i++) {
    cin >> val;
    vec_pair.push_back({val, i});
  }
  sort(vec_pair.begin(), vec_pair.end());
  long long int start = 1, end = n, ans = 0;
  for (long long int i = n - 1; i >= 0; i--) {
    long long int f = (vec_pair[i].first) * (abs(vec_pair[i].second - start));
    long long int s = (vec_pair[i].first) * (abs(vec_pair[i].second - end));
    if (f > s) {
      ans += f;
      start++;
    } else {
      ans += s;
      end--;
    }
  }
  cout << ans << endl;
  return 0;
}
