
#include <iostream>
#include <vector>
#include <array>
#include <set>
#include <cmath>
#include <string>
#include <sstream>
#include <queue> 
#include <list>
#include <stack>
#include <unordered_map>
#include <algorithm> 
#include <numeric>  
#include <functional>
#include <cassert>
#include <bitset>
#include <cctype>  
#include <iomanip> 
#include <limits>  
#include <regex>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt, less) : 
// if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*")) ) std::cout << "YES" << std::endl;

#define rep(i,n)  for(int i = 0; i < (int)(n);    i++)
#define sz(x)     ((int)(x).size())
#define all(x)    (x).begin(),(x).end()

using ll = long long;

ll gcd ( ll a, ll b ) { if ( b == 0 ) return a; else return gcd ( b, a%b ); }
ll lcm ( ll a, ll b ) { return a * b / gcd(a, b); }

int N;
std::vector<ll> A, a;
ll dp[2001][2001][2001];

ll dfs(ll k, ll l, ll r) {
  if ( k >= N ) return 0;
  if ( dp[k][l][r] != 0 ) return dp[k][l][r];
  ll x = a[k];
  ll d1 = std::abs(l-x);
  ll d2 = std::abs(r-x);
  //if ( d1 == d2 ) std::cout << x << " ( " << A[x] << " ) : " << l << " or " << r << " ; " << d1 << " = " << A[x]*d1 << std::endl;
  //if ( d1 >  d2 ) std::cout << x << " ( " << A[x] << " ) : " << l << " ; " << d1 << " = " << d1 * A[x] << std::endl;
  //if ( d1 <  d2 ) std::cout << x << " ( " << A[x] << " ) : " << r << " ; " << d2 << " = " << d2 * A[x] << std::endl;
  if ( d1 == d2 ) return dp[k][r][l] = std::max(dfs(k+1, l+1, r), dfs(k+1, l, r-1)) + A[x]*d1;
  if ( d1 >  d2 ) return dp[k][l][r] = dfs(k+1, l+1, r) + A[x]*d1;
  if ( d1 <  d2 ) return dp[k][l][r] = dfs(k+1, l, r-1) + A[x]*d2;
  return 0;
}

int main (void) {
  std::cin.tie(0);  std::ios::sync_with_stdio(false);
  std::cin >> N;
  A.resize(N); a.resize(N);
  rep(i,N) std::cin >> A[i]; 
  rep(i,N) a[i] = i;
  std::sort( all(a), [&](ll x, ll y) { return A[x] > A[y]; } ); 
  std::cout << dfs(0, 0, N-1) << std::endl;
  return 0;
}

