#include <bits/stdc++.h>
const int MOD = 1e9 + 7;
const int iINF = 2147483647 / 2;
const long long int llINF = 9223372036854775807 / 2;
using namespace std;
using ll = long long int;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
bool paircomp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  if (a.first == b.first) return a.second < b.second;
  return a.first < b.first;
}
ll POW(ll n, ll m) {
  if (m == 0) {
    return 1;
  } else if (m % 2 == 0) {
    ll tmp = POW(n, m / 2);
    return (tmp * tmp);
  } else {
    return (n * POW(n, m - 1));
  }
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N;
  cin >> N;
  vl A(N);
  vector<tuple<ll, ll, ll>> P(N);
  for (ll i = 0; i < (N); i++) {
    cin >> A[i];
    P[i] = make_tuple(A[i], i, i);
  }
  sort((P).begin(), (P).end(), greater<tuple<ll, ll, ll>>());
  ll ans = 0;
  for (ll k = 0; k < (10); k++) {
    for (ll i = 0; i < (N); i++) {
      ll score = 0;
      ll index = 0;
      for (ll j = 0; j < (N); j++) {
        ll s = abs(get<1>(P[i]) - get<2>(P[j])) * get<0>(P[i]);
        s += abs(get<2>(P[i]) - get<1>(P[j])) * get<0>(P[j]);
        s -= abs(get<2>(P[i]) - get<1>(P[i])) * get<0>(P[i]);
        s -= abs(get<2>(P[j]) - get<1>(P[j])) * get<0>(P[j]);
        if (s > score) {
          score = s;
          index = j;
        }
      }
      ll ipos = get<2>(P[i]);
      ll indexpos = get<2>(P[index]);
      if (score > 0) {
        P[i] = make_tuple(get<0>(P[i]), get<1>(P[i]), indexpos);
        P[index] = make_tuple(get<0>(P[index]), get<1>(P[index]), ipos);
      }
    }
  }
  for (ll i = 0; i < (N); i++) {
    ans += abs(get<1>(P[i]) - get<2>(P[i])) * get<0>(P[i]);
  }
  cout << ans << endl;
  return 0;
}
