6
8 6 9 1 2 1#include<bits/stdc++.h>

using namespace std;

const int mod = 1e9 + 7;

long long rec(int left, int right, vector<vector<long long>>& dp, vector<pair<int, int>>& infant){
  if(left > right) return 0;
  if(dp[left][right] != -1) return dp[left][right];
  
  long long l = rec(left + 1, right, dp, infant);
  long long r = rec(left, right - 1, dp, infant);
  
  l += (long long)infant[right - left].first * abs(infant[right - left].second - left);
  r += (long long)infant[right - left].first * abs(infant[right - left].second - right);
  
  if(l < r){
    dp[left][right] = r;
  }else{
    dp[left][right] = l;
  }
  
  return dp[left][right];
}

int main(){
  int N;
  cin >> N;
  
  vector<vector<long long>> dp(N, vector<long long>(N, -1));
  vector<pair<int, int>> infant(N);
  long long joy = 0;
  for(int i = 0; i < N; i++){
    int A;
    cin >> A;
    infant[i] = make_pair(A, i);
  }
  
  sort(infant.begin(), infant.end());
  
  cout << rec(0, N - 1, dp, infant) << endl;
  
  return 0;
}