#include <bits/stdc++.h>
using namespace std;
const long long N = 2e3 + 50;
const long long MOD = 1e9 + 7;
int c;
long long n, a[N], vis[N];
pair<int, int> t[N];
long long cal(int pos, int step) {
  int ans = 0;
  sort(t, t + c);
  for (int i = c - 1; i >= 0; i--) {
    ans = ans + abs(pos - t[i].second) * t[i].first;
    pos += step;
  }
  return ans;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  if (n % 2 == 0) {
    c = 0;
    for (int i = 1; i <= n / 2; i++) {
      t[c++] = make_pair(a[i], i);
    }
    long long r1 = cal(n, -1);
    c = 0;
    for (int i = 1 + n / 2; i <= n; i++) {
      t[c++] = make_pair(a[i], i);
    }
    long long r2 = cal(1, 1);
    cout << r1 + r2 << endl;
  } else {
    c = 0;
    for (int i = 1; i <= (n + 1) / 2; i++) {
      t[c++] = make_pair(a[i], i);
    }
    long long r1 = cal(n, -1);
    c = 0;
    for (int i = (n + 3) / 2; i <= n; i++) {
      t[c++] = make_pair(a[i], i);
    }
    long long r2 = cal(1, 1);
    long long ret_1 = r1 + r2;
    c = 0;
    for (int i = 1; i <= (n - 1) / 2; i++) {
      t[c++] = make_pair(a[i], i);
    }
    r1 = cal(n, -1);
    c = 0;
    for (int i = (n + 1) / 2; i <= n; i++) {
      t[c++] = make_pair(a[i], i);
    }
    r2 = cal(1, 1);
    long long ret_2 = r1 + r2;
    cout << max(ret_1, ret_2) << endl;
  }
  return 0;
}
