#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<pair<long long int, long long int> > z;
  for (int i = 0; i < n; i++) z.push_back(make_pair(a[i], i));
  sort(z.begin(), z.end());
  reverse((z).begin(), (z).end());
  int p1 = 0, p2 = n - 1;
  long long int ans = 0;
  for (int i = 0; i < n; i += 2) {
    if (z[i].first * abs(z[i].second - p1) +
            z[i + 1].first * abs(z[i + 1].second - p2) >
        z[i].first * abs(z[i].second - p2) +
            z[i + 1].first * abs(z[i + 1].second - p1))
      ans += z[i].first * abs(z[i].second - p1) +
             z[i + 1].first * abs(z[i + 1].second - p2);
    else
      ans += z[i].first * abs(z[i].second - p2) +
             z[i + 1].first * abs(z[i + 1].second - p1);
    p1++;
    p2--;
  }
  if (p1 <= p2) ans += z[n - 1].first * abs(z[n - 1].second - p1);
  cout << ans << endl;
}
