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: Wrong Answer)

#include <stdio.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef list<int> liint;
typedef pair<int, int> pint;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T, class C> void chmax(T& a, C b) { a > b ? : a = b; }
template<class T, class C> void chmin(T& a, C b) { a < b ? : a = b; }



int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N;
    cin >> N;
    vll A(N);
    rep(i, N) cin >> A[i];

    v2ll score(N, vll(N));
    vector<pair<ll, pair<int, int>>> scoreidx;
    rep(i, N) {
        rep(j, N) {
            score[i][j] = A[i] * abs(i - j);
            scoreidx.push_back(make_pair(score[i][j], make_pair(i, j)));
        }
    }
    sort(scoreidx.begin(), scoreidx.end(), greater< pair<ll, pair<int, int>>>());
    unordered_set<int> already_src, already_dst;
    ll ans1 = 0;

    bool is_back = false;
    int j1 = 0, j2 = N - 1;
    rep(j, N) {

        int k;
        if (is_back == false) {
            k = j1;
            j1++;
            is_back = true;
        }
        else {
            k = j2;
            j2--;
            is_back = false;
        }
        ll max_s = 0;
        ll max_i = 0;
        rep(i, N) {
            if (already_src.count(i) > 0) continue;
            if (max_s < score[i][k]) {
                max_s = max(max_s, score[i][k]);
                max_i = i;
            }
        }
        ans1 += max_s;
        already_src.insert(max_i);
    }


    is_back = true;
    ll ans2 = 0;
    already_src.clear();
    j1 = 0; j2 = N - 1;
    rep(j, N) {

        int k;
        if (is_back == false) {
            k = j1;
            j1++;
            is_back = true;
        }
        else {
            k = j2;
            j2--;
            is_back = false;
        }
        ll max_s = 0;
        ll max_i = 0;
        rep(i, N) {
            if (already_src.count(i) > 0) continue;
            if (max_s < score[i][k]) {
                max_s = max(max_s, score[i][k]);
                max_i = i;
            }
        }
        ans2 += max_s;
        already_src.insert(max_i);
    }


    
    cout << max(ans1, ans2) << endl;
    return 0;
}