#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > dat;
vector<long long> rui;
long long N;
long long ch(long long ct, long long ans, vector<pair<long long, long long> > A,
             vector<pair<long long, long long> > B) {
  for (long long(i) = long long(ct); (i) < long long(N); (i)++) {
    if (rui[i] == (rui[N] - rui[i + 1])) {
      long long aa, bb;
      A.push_back(dat[i]);
      aa = ch(ct + 1, ans, A, B);
      A.pop_back();
      B.push_back(dat[i]);
      bb = ch(ct + 1, ans, A, B);
      ans = ((aa) > (bb) ? (aa) : (bb));
      return ans;
    } else if (rui[i] > (rui[N] - rui[i + 1])) {
      A.push_back(dat[i]);
    } else {
      B.push_back(dat[i]);
    }
  }
  sort(A.begin(), A.end(), std::greater<pair<long long, long long> >());
  sort(B.begin(), B.end(), std::greater<pair<long long, long long> >());
  for (long long(i) = long long(0); (i) < long long(A.size()); (i)++) {
    ans += A[i].first *
           ((i - A[i].second) > (0) ? (i - A[i].second) : -(i - A[i].second));
  }
  for (long long(i) = long long(0); (i) < long long(B.size()); (i)++) {
    ans += B[i].first * (((N - i - 1) - B[i].second) > (0)
                             ? ((N - i - 1) - B[i].second)
                             : -((N - i - 1) - B[i].second));
  }
  return ans;
}
int main() {
  cin >> N;
  long long ans = 0;
  rui.resize(N + 1);
  for (long long(i) = long long(0); (i) < long long(N); (i)++) {
    long long a;
    cin >> a;
    dat.emplace_back(pair<long long, long long>(a, i));
    rui[i + 1] = rui[i] + a;
  }
  vector<pair<long long, long long> > A, B;
  ans = ch(0, 0, A, B);
  cout << (ans) << endl;
  return 0;
}
