#include <bits/stdc++.h>
using namespace std;
bool lft;
bool problem;
int trymax(vector<int> a, vector<int> npos, vector<int> opos) {
  long long trym = -1;
  for (int i = 0; i < (int)a.size(); ++i) {
    long long lmax, rmax;
    rmax = 1LL * a[i] * abs(opos[i] - npos.front());
    lmax = 1LL * a[i] * abs(opos[i] - npos.back());
    long long omax = max(lmax, rmax);
    trym = max(trym, omax);
  }
  return trym;
}
pair<long long, int> maxsc(vector<int> a, vector<int> npos, vector<int> opos) {
  pair<long long, int> top = {-1, 0};
  problem = false;
  for (int i = 0; i < (int)a.size(); ++i) {
    long long lmax, rmax;
    rmax = 1LL * a[i] * abs(opos[i] - npos.front());
    lmax = 1LL * a[i] * abs(opos[i] - npos.back());
    long long omax = max(lmax, rmax);
    if (omax > top.first) {
      if (rmax > lmax)
        lft = true;
      else if (rmax == lmax) {
        problem = true;
      } else
        lft = false;
      top.first = omax;
      top.second = i;
    } else if (omax == top.first) {
      if (a[i] < a[top.second]) {
        top.first = omax;
        top.second = i;
      }
    }
  }
  return top;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> pos(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    pos[i] = i;
  }
  vector<int> npos(pos.begin(), pos.end());
  long long ans = 0;
  for (int i = 0; a.empty() == false; ++i) {
    pair<long long, int> top = maxsc(a, npos, pos);
    ans += top.first;
    if (problem) {
      vector<int> try1(npos.begin(), npos.end()),
          try2(npos.begin(), npos.end());
      for (int j = 0; j < (int)try1.size(); ++j) {
        if (j < top.second)
          try1[j]++;
        else
          break;
      }
      for (int j = (int)try2.size() - 1; j >= 0; --j) {
        if (j > top.second)
          try2[j]--;
        else
          break;
      }
      if ((trymax(a, try1, pos)) > trymax(a, try2, pos))
        lft = true;
      else
        lft = false;
    }
    if (lft) {
      for (int j = 0; j < (int)npos.size(); ++j) {
        if (j < top.second)
          npos[j]++;
        else
          break;
      }
    } else {
      for (int j = (int)npos.size() - 1; j >= 0; --j) {
        if (j > top.second)
          npos[j]--;
        else
          break;
      }
    }
    a.erase(a.begin() + top.second);
    pos.erase(pos.begin() + top.second);
    npos.erase(npos.begin() + top.second);
  }
  cout << ans << "\n";
  return 0;
}
