#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
  int n;
  cin >> n;
  long long a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  int k = max_element(a, a + n) - a;
  long long ans1 = 0;
  pair<long long, int> p[n];
  for (int i = 0; i < n; i++) p[i] = make_pair(a[i], i);
  sort(p, p + k, greater<pair<long long, int>>());
  sort(p + k, p + n, greater<pair<long long, int>>());
  for (int i = 0; i < k; i++) {
    ans1 += p[i].first * abs(n - 1 - i - p[i].second);
  }
  for (int i = k; i < n; i++) {
    ans1 += p[i].first * abs(0 + i - k - p[i].second);
  }
  long long ans2;
  pair<long long, int> q[n];
  for (int i = 0; i < n; i++) q[i] = make_pair(a[i], i);
  sort(q, q + k + 1, greater<pair<long long, int>>());
  sort(q + k + 1, q + n, greater<pair<long long, int>>());
  for (int i = 0; i < k + 1; i++) {
    ans2 += q[i].first * abs(n - 1 - i - q[i].second);
  }
  for (int i = k + 1; i < n; i++) {
    ans2 += q[i].first * abs(0 + i - k - 1 - q[i].second);
  }
  cout << max(ans1, ans2) << endl;
  return 0;
}
