#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
using matrix = vector<vector<uint>>;
template <typename T>
using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acos(-1);
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> B(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    B[i] = i;
  }
  ll ans = 0;
  for (int i = 0; i < N; ++i) {
    int maidx = i;
    ll ma = 0;
    for (int j = 0; j < N; ++j) {
      if (i == j) continue;
      ll tmp = -A[i] * abs(B[i] - i) - A[j] * abs(B[j] - j);
      tmp += A[i] * abs(B[j] - i) + A[j] * abs(B[i] - j);
      if (ma < tmp) {
        ma = tmp;
        maidx = j;
      }
    }
    ans += ma;
    swap(B[i], B[maidx]);
  }
  cout << ans << endl;
  return 0;
}
