include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
pair <ll,ll> a[2000];
void solve()
{
	ll n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i].first, a[i].second=i;
	ll l = 0, r = 1, mx=0, pos=0, sum=0;
	while (l + r != n)
	{
		for (int i = l; i <= n-r; i++)
			if (mx < a[i].first)
				mx = a[i].first, pos = i;
		if (abs(a[pos].second - l) > abs(a[pos].second - r))
		{
			swap(a[l], a[pos]);
			l++;
		}
		else
		{
			swap(a[n - r], a[pos]);
			r++;
		}
		mx = 0, pos = 0;
	}
	for (int i = 0; i < n; i++)
		sum += abs(a[i].second - i) * a[i].first;
	cout << sum;
}
int main()
{
//	int t;
//	cin >> t;
//	while (t-- > 0)
		solve();
}
