#include <bits/stdc++.h>
using namespace std;
int main() {
  long n, k, top, kotae, basyo;
  cin >> n;
  vector<pair<long, long>> vec(n);
  vector<pair<long, long>> ans(0);
  for (long i = 0; i < n; i++) {
    cin >> vec.at(i).first;
    vec.at(i).second = i;
  }
  ans.push_back({vec.at(1).first, vec.at(1).second});
  ans.push_back({vec.at(0).first, vec.at(0).second});
  for (long i = 2; i < n; i++) {
    k = 0;
    top = 0;
    basyo = 0;
    ans.push_back({vec.at(i).first, vec.at(i).second});
    for (long j = i; 0 < j; j--) {
      k = k + ans.at(i).first +
          ans.at(j - 1).first * (abs(j - ans.at(j - 1).second)) -
          ans.at(j - 1).first * (abs(j - 1 - ans.at(j - 1).second));
      if (k > top) {
        top = k;
        basyo = j;
      }
    }
    for (long j = i; basyo <= j; j--) {
      swap(ans.at(j).first, ans.at(j - 1).first);
      swap(ans.at(j).second, ans.at(j - 1).second);
    }
  }
  kotae = 0;
  for (long i = 0; i < n; i++) {
    kotae = kotae + ans.at(i).first * abs(i - ans.at(i).second);
  }
  cout << kotae;
}
