#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_(i, n, m) for (int i = n; i < (int)(m); i++)
#define all(v) v.begin(), v.end()
#define int long long
#define stoi stoll
//#define _GLIBCXX_DEBUG

int move(int N, auto &A, int k, int s, int t, int ans) {
  int a = A[k].first, i = A[k].second;
  //cout << a << " " << i << endl;
  if(k == N-1) {
    return ans+a*abs(i-t);
  }
  if(abs(i-s) < abs(i-t)) {
    return move(N, A, k+1, s, t-1, ans+a*abs(i-t));
  }
  if(abs(i-s) > abs(i-t)) {
    return move(N, A, k+1, s+1, t, ans+a*abs(i-s));
  }
  if(abs(i-s) == abs(i-t)) {
    return max(move(N, A, k+1, s, t-1, ans+a*abs(i-t)),
               move(N, A, k+1, s+1, t, ans+a*abs(i-s)));
  }
}

signed main() {
  int N; cin >> N;
  vector<pair<int,int>> A(N, pair<int,int>());
  rep(i, N) {
    int a; cin >> a;
    A[i] = make_pair(a, i);
  }
  sort(all(A));
  reverse(all(A));
  
  cout << move(N, A, 0, 0, N-1, 0) << endl;
  
}
