#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long 

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>, 
hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;


typedef unsigned long  ull;

int gen_base(const int before, const int after) {
    auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
    std::mt19937 mt_rand(seed);
    int base = std::uniform_int_distribution<int>(before+1, after)(mt_rand);
    return base % 2 == 0 ? base-1 : base;
}
 
map<string,pair<int, ull>> polyDp;

struct PolyHash {
    static const int mod = (int)1e9+123; 
    static std::vector<int> pow1;        
    static std::vector<ull> pow2;        
    static int base;                     
 
    static inline int diff(int a, int b) { 
        return (a -= b) < 0 ? a + mod : a;
    }
 
    std::vector<int> pref1; 
    std::vector<ull> pref2; 
 
    PolyHash(const std::string& s): pref1(s.size()+1u, 0), pref2(s.size()+1u, 0){
        assert(base < mod);
        const int n = s.size();
        while ((int)pow1.size() <= n) {
            pow1.push_back(1LL * pow1.back() * base % mod);
            pow2.push_back(pow2.back() * base);
        }
        for (int i = 0; i < n; ++i) { 
            assert(base > s[i]);
            pref1[i+1] = (pref1[i] + 1LL * s[i] * pow1[i]) % mod;
            pref2[i+1] = pref2[i] + s[i] * pow2[i];
        }
    }
 
    inline std::pair<int, ull> operator()(const int pos, const int len, const int mxPow = 0) const {  

        int hash1 = pref1[pos+len] - pref1[pos];
        ull hash2 = pref2[pos+len] - pref2[pos];
        if (hash1 < 0) hash1 += mod;
        if (mxPow != 0) {
            hash1 = 1LL * hash1 * pow1[mxPow-(pos+len-1)] % mod;
            hash2 *= pow2[mxPow-(pos+len-1)];
        }
        return std::make_pair(hash1, hash2);
    }
};

int PolyHash::base((int)1e9+7);    
std::vector<int> PolyHash::pow1{1};
std::vector<ull> PolyHash::pow2{1};


long int mxPow;
vector<long int> v;
long int n;

map<pair<int, ull>,long int> dp;
map<long int,long int> m;

long int solve(long int idx,string &s){
  if(idx == n)
    return 0;

  pair<int, ull> val;
  if(polyDp[s].first != 0 || polyDp[s].second != 0){
    val = polyDp[s];
  }
  else{
    PolyHash hash_a(s);
    val = hash_a(0, n, mxPow);
  }

  if(dp[val])
    return dp[val];

  long int ans = 0;

  for(long int i = 0; i < s.size(); i++){
    if(s[i] == '0'){
      long int newPos = i + 1;
      long int oldPos = idx + 1;
      s[newPos - 1] = '1';
      ans = max(ans, llabs(newPos - oldPos) * v[idx] + solve(idx + 1,s));
      s[newPos - 1] = '0';
    }
  }

  return dp[val] = ans;
}

int main(){
   IOS;
 /*    #ifndef ONLINE_JUDGE
         freopen("in.txt",  "r",  stdin);
         freopen("out.txt", "w", stdout);
     #endif */

   cin>>n;

   v.resize(n);
   for(long int i = 0; i < n; i++)
    cin>>v[i];

   for(long int i = 0; i < n; i++)
      m[v[i]] = i + 1;

    mxPow = n;
    PolyHash::base = gen_base(256, PolyHash::mod);

    string s;
    for(long int i = 0; i < n; i++)
      s.push_back('0');

    cout<<solve(0,s);

   return 0;
}