#include <bits/stdc++.h>
using namespace std;
using P = pair<long long, long long>;
signed main() {
  long long n;
  cin >> n;
  vector<long long> A(n);
  for (long long i = 0; i < n; i++) cin >> A[i];
  vector<P> child(n);
  for (long long i = 0; i < n; i++) {
    child[i] = make_pair(A[i], i);
  }
  sort(child.begin(), child.end());
  reverse(child.begin(), child.end());
  long long left = 0;
  long long right = n - 1;
  long long ans = 0;
  ans += child[0].first * abs(child[0].second - left);
  left++;
  for (long long i = 1; i < n; i++) {
    if (abs(child[i].second - left) > abs(child[i].second - right)) {
      ans += child[i].first * abs(child[i].second - left);
      left++;
    } else {
      ans += child[i].first * abs(child[i].second - right);
      right--;
    }
  }
  left = 0;
  right = n - 1;
  long long wans = 0;
  wans += child[0].first * abs(child[0].second - right);
  right--;
  for (long long i = 1; i < n; i++) {
    if (abs(child[i].second - left) > abs(child[i].second - right)) {
      wans += child[i].first * abs(child[i].second - left);
      left++;
    } else {
      wans += child[i].first * abs(child[i].second - right);
      right--;
    }
  }
  cout << max(wans, ans) << endl;
}
