#include<cstdio>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<bitset>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<complex>
#define inf 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define pb push_back
#define ll long long
#define pii pair<int,int> 
const int N=1e6+10;
const int mod=1e9+7;
using namespace std;


#define int long long
const int INF=0x3f3f3f3f3f3f3f3f;
struct edge{
  int to,cap,cost,rev;
  edge(){}
  edge(int to,int _cap,int _cost,int _rev):to(to),cap(_cap),cost(_cost),rev(_rev){}
};
struct Min_Cost_Max_Flow{
  int n,h[N],dis[N],prev[N],pree[N];
  vector<edge> G[N];
  void init(int _n){
    n=_n;
    for(int i=0;i<=n;++i) G[i].clear();
  }
  void add(int from,int to,int cap,int cost){
    G[from].push_back(edge(to,cap,cost,G[to].size()));
    G[to].push_back(edge(from,0,-cost,G[from].size()-1));
  }
  int min_cost_max_flow(int s,int t,int f,int &flow){
    int res=0; flow=0;
    fill(h,h+1+n,0);
    while(f){
      priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>> >q;
      fill(dis,dis+1+n,INF);
      dis[s]=0; q.push({0,s});
      while(!q.empty()){
        pair<int,int> now=q.top(); q.pop();
        int v=now.second;
        if(dis[v]<now.first) continue;
        for(int i=0;i<G[v].size();++i){
          edge& e=G[v][i];
          if(e.cap>0&&dis[e.to]>dis[v]+e.cost+h[v]-h[e.to]){
            dis[e.to]=dis[v]+e.cost+h[v]-h[e.to];
            prev[e.to]=v;
            pree[e.to]=i;
            q.push({dis[e.to],e.to});
          }
        }
      }
      if(dis[t]==INF) break;
      for(int i=0;i<=n;++i) h[i]+=dis[i];
      int d=f;
      for(int v=t;v!=s;v=prev[v]) d=min(d,G[prev[v]][pree[v]].cap);
      f-=d; flow+=d; res+=d*h[t];
      for(int v=t;v!=s;v=prev[v]){
        edge& e=G[prev[v]][pree[v]];
        e.cap-=d;
        G[v][e.rev].cap+=d;
      }
    }
    return res;
  }
}MCMF;

int a[N];
int32_t main()
{ 
  int t=1;//scanf("%d",&t);
  while(t--){
    int n; cin>>n;
    for(int i=1;i<=n;++i) cin>>a[i];
    int s1=0,s2=2*n+1,t=2*n+2;
    MCMF.init(t);
    //MCMF.add(s1,s2,k,0);
    for(int i=1;i<=n;++i){
      for(int j=1;j<=n;++j)
      MCMF.add(i,j+n,1,-a[i]*abs(i-j));
    }
    
    for(int i=1;i<=n;++i){
      MCMF.add(s1,i,1,0);
      MCMF.add(i+n,t,1,0);
    }
    int flow;
    cout<<-MCMF.min_cost_max_flow(s1,t,INF,flow)<<endl;
  }
return 0;
}