#include<bits/stdc++.h>
using namespace std;
int32_t main()
{long long n,sum=0;
  cin>>n;
 vector<pair<long long,long long>>a(n);
 for(long long i=0;i<n;i++)cin>>a[i].first,a[i].second=i;
 sort(a.begin(),a.end());
 for(long long i=0;i<n/2;i++)
 {
   sum+=(a[i].first*abs(a[i].second-a[n-1-i].second));
 }
 if(n%2)s=(n/2)+1;
 else if(!n%2)s=(n/2);
 for(long long i=s;i<n;i++)
 {
  sum+=(a[i].first*abs(a[i].second-a[n-1-i].second));
 }
  cout<<sum;
  return 0;
}