#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
  #include "LOCAL_DEBUG.hpp"
#endif
#define int long long

template<class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template<class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
    t = v;
}
template<class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
    for (auto &e : t) fill(e, v);
}
// auto v = make_vec<int>(h, w);
// fill(v, 0);

using P = pair<int, int>;

signed main(){

  int n; cin >> n;
  vector<int> a(n);
  vector<P> p(n);
  for(int i = 0; i < n; i++){
    cin >> a[i];
    p[i] = {a[i], i};
  }
  sort(p.rbegin(), p.rend());

  auto dp = make_vec<int>(n+1, n+1);
  auto dfs = [&](auto&& dfs, int l, int r, int i)->void{
    if(i == n || l + r == n) return;
    dp[l+1][r] = max(dp[l+1][r], dp[l][r] + p[i].first * (p[i].second - l));
    dp[l][r+1] = max(dp[l][r+1], dp[l][r] + p[i].first * ((n-1-r) - p[i].second));
    if(p[i].second - l > 0) dfs(dfs, l+1, r, i+1);
    if((n-1-r) - p[i].second > 0) dfs(dfs, l, r+1, i+1);
  };
  dfs(dfs, 0, 0, 0);

  int ans = 0;
  for(int i = 0; i <= n; i++){
    for(int j = 0; j <= n; j++){
      if(i+j == n) ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << endl;

  return 0;
}