#pragma GCC target("avx512")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include<atcoder/mincostflow>

using namespace std;
using ll = long long;
inline int ri() { int n; cin >> n; return n;}
const ll INF = 1e14;

int main()
{
	int n; cin >> n;
	vector<ll> a(n);
	for(int i = 0; i < n; i++)cin >> a[i];
	atcoder::mcf_graph<int,ll> mf(2 * n + 2);
	int S = 2 * n, T = 2 * n + 1;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			mf.add_edge(i, j + n, 1, INF - a[i] * abs(i - j));
		}
		mf.add_edge(S, i, 1, 0);
		mf.add_edge(i + n, T, 1, 0);
	}

	auto flow = mf.flow(S, T, n);
	cout << INF * n - flow.second << '\n';

}