#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, start = 0, end = 0, actual_i = 0, ans = 0, judge = 0;
  cin >> n;
  long long activity[n];
  pair<long long, long long> a_i_pair[n];
  for (long long i = 0; i < n; i++) {
    cin >> a_i_pair[i].first;
    a_i_pair[i].second = i;
    activity[i] = a_i_pair[i].first;
  }
  sort(a_i_pair, a_i_pair + n);
  reverse(a_i_pair, a_i_pair + n);
  for (long long i = 0; i < n; i++) {
    long long left = a_i_pair[i].first * (abs(start - a_i_pair[i].second)),
              right =
                  a_i_pair[i].first * (abs(n - 1 - end - a_i_pair[i].second));
    if (left > right) {
      if (i == actual_i) {
        actual_i++;
        start++;
        ans += left;
      } else {
        i = actual_i;
        ans += a_i_pair[i].first * (abs(n - 1 - end - a_i_pair[i].second));
        end++;
        actual_i++;
      }
    } else if (left < right) {
      if (i == actual_i) {
        end++;
        actual_i++;
        ans += right;
      } else {
        i = actual_i;
        ans += a_i_pair[i].first * (abs(start - a_i_pair[i].second));
        start++;
        actual_i++;
      }
    } else if (i == (n - 1)) {
      i = actual_i;
      ans += a_i_pair[i].first * (abs(start - a_i_pair[i].second));
      start++;
      actual_i++;
    }
  }
  cout << ans;
  return 0;
}
