#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long n) {
  a %= 1000000007;
  if (n == 1) return a;
  if (n == 0) return 1;
  if (n % 2)
    return (a * (power((a * a) % 1000000007, n / 2) % 1000000007)) % 1000000007;
  return power((a * a) % 1000000007, n / 2) % 1000000007;
}
const long long inf = (long long)1e18;
long long inverse(long long x) { return power(x, 1000000007 - 2) % 1000000007; }
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  vector<pair<long long, long long> > v;
  for (long long i = 1; i < n + 1; ++i) {
    long long x;
    cin >> x;
    v.push_back({x, i});
  }
  sort(v.begin(), v.end());
  long long i1 = 1, i2 = n, ans = 0;
  for (long long i = n - 1; i >= 0; i -= 2) {
    ans += max(abs(i1 - v[i].second) * v[i].first +
                   abs(i2 - v[i - 1].second) * v[i - 1].first,
               abs(i2 - v[i].second) * v[i].first +
                   abs(i1 - v[i - 1].second) * v[i - 1].first);
    i1++;
    i2--;
  }
  cout << ans;
  return 0;
}
