#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
#define pi 3.141592653589793
#define mod 998244353
#define pb push_back
#define all(v) v.begin(),v.end()
#define tc int t;cin>>t;while(t--)
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fast_io ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc_g int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;

int main() {
	fast_io;
	int n;
	cin >> n;
	vl a(n), pos(n);
	vector<pll> x(n);
	for (int i = 0;i < n;i++) {
		cin >> a[i];
		pos[i] = i;
		x[i] = {a[i], i};
	}
	ll ans = 0;
	sort(all(x), greater<pll>());
	for (auto b : x) {
		int i = pos[b.second];
		//cout << i << '\n';
		ll add = LLONG_MIN;
		ll index;
		for (int j = 0;j < n;j++) {
			ll profit = a[j] * abs(i - pos[j]) - a[j] * abs(pos[j] - j) + a[i] * abs(j - pos[i]) - a[i] * abs(pos[i] - i);
			//cout << profit << ' ';
			if (profit > add) {
				add = profit;
				index = j;
			}
		}
		if (add > 0) {
			swap(a[i], a[index]);
			swap(pos[i], pos[index]);
			ans += add;
		}
		//for (auto el : a)
				//cout << el << ' ';
			//cout << '\n';
	}
	cout << ans << '\n';
}
