#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

int main() {
	int n; scanf("%d", &n); std::vector<i64> a(n);
	for(int i = 0; i < n; i++) scanf("%lld", &a[i]);

	std::vector<int> order(n);
	std::iota(begin(order), end(order), 0);
	auto f = [&](int i, int j) {
		int x = order[i], y = order[j];
		i64 A = a[x] * std::abs(x - i) + a[y] * std::abs(y - j);
		i64 B = a[y] * std::abs(y - i) + a[x] * std::abs(x - j);
		return B - A;
	};
	for(int i = 0; i < n; i++) {
		int imax = i;
		for(int j = 0; j < n; j++) {
			if(f(i, imax) >= f(i, j)) continue;
			imax = j;
		}
		std::swap(order[i], order[imax]);
	}

	i64 ans = 0;
	for(int i = 0; i < n; i++) ans += a[order[i]] * std::abs(order[i] - i);
	printf("%lld\n", ans);
	return 0;
}
