#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<long long int> a(n);
  vector<pair<long long int, int>> v, vv;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    v.push_back(make_pair((long long int)a[i] * i, i));
    vv.push_back(make_pair((long long int)(n - 1 - i) * a[i], i));
  }
  vector<pair<long long int, int>> p;
  for (int i = 0; i < n; i++) {
    p.push_back(make_pair(a[i], i));
  }
  sort(p.begin(), p.end());
  long long int res = 0;
  vector<bool> done(n, 0);
  for (int i = 0; i < n; i++) {
    sort(v.rbegin(), v.rend());
    sort(vv.rbegin(), vv.rend());
    if (v[0].first > vv[0].first ||
        (v[0].first == vv[0].first && a[v[0].second] >= a[vv[0].second])) {
      res += v[0].first;
      done[v[0].second] = 1;
      for (int j = 0; j < n; j++) {
        if (done[v[j].second]) {
          v[j].first = -1e9;
        } else {
          v[j].first -= a[v[j].second];
        }
        if (done[vv[j].second]) {
          vv[j].first = -1e9;
        }
      }
    } else {
      res += vv[0].first;
      done[vv[0].second] = 1;
      for (int j = 0; j < n; j++) {
        if (done[v[j].second]) {
          v[j].first = -1e9;
        }
        if (done[vv[j].second]) {
          vv[j].first = -1e9;
        } else {
          vv[j].first -= a[vv[j].second];
        }
      }
    }
  }
  printf("%lld\n", res);
}
