//
//  main.cpp
//  CoderProj
//
//  Created by Minoru Hayashi on 2020/04/04.
//  Copyright © 2020 Minoru Hayashi. All rights reserved.
//
//  ABC163
//
//

#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for (int i=0; i<(n); i++)
#define REP2(i,x,n) for (int i=x; i<(n); i++)
#define ALL(n) begin(n), end(n)

using ll = long long;

template<class T> inline bool chmin(T& a, T b){ if(a>b){a=b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b){ if(a<b){a=b; return 1;} return 0;}

// const ll INF = 1LL<<60;
// const int INF = 1<<29;
// const int MOD = 1000000007;
//#define __DBG__


void solve(void){
  
  int N;
  cin >> N;
  
  vector<int> A(N+1,0);
  vector<tuple<int,int,int>> P(N+1);
  vector<int> R(N+1,0);
  vector<int> S(N+1,1);
  
  REP2(i,1,N+1){
    cin >> A[i];
    REP2(j,1,N+1){
      P.push_back(make_tuple(A[i]*abs(i-j),i,j));
    }
  }
  sort(ALL(P), greater<>());
/*
  for(auto v:P){
    cout << get<0>(v) << " " << get<1>(v) << " " << get<2>(v) << endl;
  }
*/
  ll count=N*(N+1)/2;
  ll ans=0;
  
  while(count>0){
    REP(i,P.size()){
      
     // cout << i << " " << get<1>(P[i]) << " " << get<2>(P[i]) << endl;
      
      if(R[get<2>(P[i])]>0 || S[get<1>(P[i])]==0){
        continue;
      }
      else
      {
//        cout << get<1>(P[i]) << "->" << get<2>(P[i]) << " +" << get<0>(P[i]) << endl;
        R[get<2>(P[i])]=get<1>(P[i]);
        S[get<1>(P[i])]=0;
        ans += get<0>(P[i]);
        count -= get<2>(P[i]);
      }
    }
  }

  cout << ans << endl;
  
  return;
   
}



#define TESTCASES 1

int main(int argc, const char * argv[]) {
  

    cin.tie(0);
    ios::sync_with_stdio(false);

    REP(i,TESTCASES){
      solve();
    }
 
    return 0;
}

