#include <bits/stdc++.h>
using namespace std;
const double pi = 3.14159265358979323846264;
const int mod = 1000000007;
int N;
long long calc(vector<int> a, vector<pair<int, int> > p) {
  long long sm = 0;
  int n = N;
  for (int i = (0); i < (n); ++i) {
    sm += 1LL * abs(p[i].second - i) * p[i].first;
  }
  return sm;
}
int main() {
  int n;
  cin >> n;
  N = n;
  vector<int> a(n);
  vector<pair<int, int> > p(n);
  for (int i = (0); i < (n); ++i) {
    cin >> a[i];
    p[i] = make_pair(a[i], i);
  }
  int l = 0, r = n - 1;
  for (int i = (0); i < (n); ++i) {
    int idx = 0, mx = 0;
    for (int j = (l); j < (r + 1); ++j) {
      if (mx < p[j].first) {
        mx = p[j].first;
        idx = j;
      }
    }
    vector<pair<int, int> > p1(p);
    vector<pair<int, int> > p2(p);
    pair<int, int> tmp = p1[idx];
    for (int i = (idx + 1) - 1; i >= (l + 1); --i) {
      p1[i] = p1[i - 1];
    }
    p1[l] = tmp;
    pair<int, int> tmp2 = p2[idx];
    for (int i = (idx); i < (r); ++i) {
      p2[i] = p2[i + 1];
    }
    p2[r] = tmp2;
    long long s1 = calc(a, p1);
    long long s2 = calc(a, p2);
    if (s1 > s2) {
      l++;
      p = p1;
    } else {
      r--;
      p = p2;
    }
  }
  cout << calc(a, p) << endl;
}
