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

int main() {
  long long n,ans;
  ans=0;
  cin>>n;
  vector<pair<long long, long long>>a(n);
  for(int i=0;i<n;i++){
    cin>>a[i].first;
    a[i].second=i+1;
  }
  sort(a.begin(),a.end());
  vector<bool>b(n);
  for(int i=0;<n;i++) b[i]=true;
  for(int i=n-1;i<=0;i--){
    long long l,r;
    for(int i=0;i<n;i++){
      if(b[i]){
        left=i+1;
        break;
      }
    }
    for(int i=0;i<n;i++){
      if(b[n-1-i]){
        right=n-i;
        break;
      }
    }
    if(abs(left-a[i].second)>abs(right-a[i].second)){
      ans+=(left-a[i].second)*a[i].first;
      b[left-1]=false;
    }
    else if(abs(left-a[i].second)<abs(right-a[i].second)){
      ans+=(right-a[i].second)*a[i].first;
      b[right-1]=false;
    }
    else{
      if(abs(left-a[i+1].second)>abs(right-a[i-1].second)){
        ans+=(right-a[i].second)*a[i].first;
        b[right-1]=false;
      }
      else{
        ans+=(left-a[i].second)*a[i].first;
        b[left-1]=false;
      }
    }
  }
  cout<<ans<<endl;
}