#include <iostream>
#include <vector>
#include <utility>
#include <cstdint>
#include <algorithm>
#include <functional>

template<typename F>
constexpr decltype(auto) fix(F&& f) {
	return[f = std::forward<F>(f)](auto&&... args) {
		return f(f, std::forward<decltype(args)>(args)...);
	};
}

using i64 = std::int64_t;

int main() {
	int n;
	std::cin >> n;

	std::vector<std::pair<i64, int>> vec(n);
	for (int i = 0; i < n; ++i) {
		i64 a;
		std::cin >> a;

		vec[i] = { a, i };
	}

	std::sort(vec.begin(), vec.end(), std::greater<>{});

	auto solve = fix([&](auto f, int idx, int l, int r) -> i64 {
		if (idx == n)
			return 0;

		int ld = std::abs(vec[idx].second - l);
		int rd = std::abs(vec[idx].second - r);

		i64 ret = 0;
		
		ret = std::max(ret, vec[idx].first * ld + f(f, idx + 1, l + 1, r));
		ret = std::max(ret, vec[idx].first * rd + f(f, idx + 1, l, r - 1));

		return ret;
	});

	std::cout << solve(0, 0, n - 1) << std::endl;
}