Problem:
Score : 500 points

Problem Statement

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.

You can rearrange these children just one time in any order you like.

When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

2 \leq N \leq 2000

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum total happiness points the children can earn.

Sample Input 1

4
1 3 4 2

Sample Output 1

20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Sample Input 2

6
5 5 6 1 1 1

Sample Output 2

58

Sample Input 3

6
8 6 9 1 2 1

Sample Output 3

85

Solution in C (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <stdlib.h>

typedef struct {
	int key, id;
} data;

void merge_sort(data x[], int n)
{
	static data y[2001] = {};
	if (n <= 1) return;
	merge_sort(&(x[0]), n/2);
	merge_sort(&(x[n/2]), (n+1)/2);
	
	int i, p, q;
	for (i = 0, p = 0, q = n/2; i < n; i++) {
		if (p >= n/2) y[i] = x[q++];
		else if (q >= n) y[i] = x[p++];
		else y[i] = (x[p].key < x[q].key)? x[p++]: x[q++];
	}
	for (i = 0; i < n; i++) x[i] = y[i];
}

void add_BIT(long long BIT[], int n, int i, int k)
{
	while (i <= n) {
		BIT[i] += k;
		i += (i & -i);
	}
}

long long sum_BIT(long long BIT[], int r)
{
	long long sum = 0;
	while (r > 0) {
		sum += BIT[r];
		r -= (r & -r);
	}
	return sum;
}

int N, A[2001], flag[2001] = {};
long long BIT[2][2001] = {};
data d[2001];

long long recursion(int k, int l, int r)
{
	if (l == r) return (long long)A[d[k].id] * abs(d[k].id - l);
	
	long long ans, tmp[2] = {(long long)A[d[k].id] * abs(d[k].id - l), (long long)A[d[k].id] * abs(d[k].id - r)};
	flag[d[k].id] = 1;
	add_BIT(BIT[0], N, d[k].id, -(d[k].key));
	add_BIT(BIT[1], N, N - d[k].id + 1, -(d[k].key));
	if (tmp[0] > tmp[1]) {
		ans = recursion(k - 1, l + 1, r) + tmp[0];
		if (tmp[0] - sum_BIT(BIT[1], N - d[k].id) < tmp[1] - sum_BIT(BIT[0], d[k].id - 1) / 10) {
			tmp[1] += recursion(k - 1, l, r - 1);
			if (tmp[1] > ans) ans = tmp[1];
		}
	} else {
		ans = recursion(k - 1, l, r - 1) + tmp[1];
		if (tmp[0] - sum_BIT(BIT[1], N - d[k].id) / 10 > tmp[1] - sum_BIT(BIT[0], d[k].id - 1)) {
			tmp[0] += recursion(k - 1, l + 1, r);
			if (tmp[0] > ans) ans = tmp[0];
		}
	}
	flag[d[k].id] = 0;
	add_BIT(BIT[0], N, d[k].id, d[k].key);
	add_BIT(BIT[1], N, N - d[k].id + 1, d[k].key);
	return ans;
}

int main()
{
	int i;
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d", &(A[i]));
	for (i = 1; i <= N; i++) {
		add_BIT(BIT[0], N, i, A[i]);
		add_BIT(BIT[1], N, N - i + 1, A[i]);
	}
	for (i = 0; i < N; i++) {
		d[i].key = A[i+1];
		d[i].id = i + 1;
	}
	merge_sort(d, N);
	
	printf("%lld\n", recursion(N - 1, 1, N));
	fflush(stdout);
	return 0;
}