#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define loop(i, a, b) for(li i=a; i<=b; i++)
#define stuff ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long li;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<li, li> lii;
typedef vector<int> vi;
typedef vector<li> vli;
typedef vector<ii> vii;
typedef vector<lii> vlii;
li mod = 1e9+7;
li expo(li base, li exponent)
{
	li ans=1;
	while(exponent)
	{
		if(exponent&1) ans = (ans*base)%mod;
		base = (base*base)%mod;
		exponent >>= 1;
	}
	return ans;
}
li gcd(li x, li y)
{
	if(y==0) return x;
	return gcd(y, x%y);
}


typedef tree<li, null_type, less<li>,
rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int main()
{
	stuff;
	// #ifndef ONLINE_JUDGE 
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	// freopen("error.txt", "w", stderr);
	// #endif

	li n;
	cin >> n;
	vector<lii> ar(n);
	loop(i, 0, n-1)
	{
		cin >> ar[i].fi;
		ar[i].se = i;
	}
	sort(ar.begin(), ar.end(), greater<lii>());
	vector<bool> vis(n, 0);
	li l, r, ptr=0, x, ans=0;
	while(ptr<n)
	{
		x = ar[ptr].se;
		l = ar[ptr].se;
		for(int i=ar[ptr].se; i>=0; i--)
		{
			if(!vis[i]) l=i;
		}
		r = ar[ptr].se;
		for(int i=ar[ptr].se; i<n; i++)
		{
			if(!vis[i]) r=i;
		}
		if(abs(x-l) >= abs(r-x))
		{
			vis[l] = 1;
			ans += ar[ptr].fi*(abs(x-l));
		}
		else
		{
			vis[r]  =1;
			ans += ar[ptr].fi*(abs(x-r));
		}
		// cout << ar[ptr].fi << " " << l << " lr " << r  << "x" << x<< endl; 
		ptr++;
	}
	cout << ans;
	return 0;
}
