#P2858. [USACO06FEB] Treats for the Cows G/S

[USACO06FEB] Treats for the Cows G/S

Description

FJ has purchased N(1N2000)N (1 \le N \le 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time.

The treats are interesting for many reasons:The treats are numbered 1N1 \sim N and stored sequentially in single file in a long box that is open at both ends. On any day, FJ can retrieve one treat from either end of his stash of treats.Like fine wines and delicious cheeses, the treats improve with age and command greater prices.The treats are not uniform: some are better and have higher intrinsic value. Treat ii has value vi(1vi1000)v_i (1 \le v_i \le 1000). Cows pay more for treats that have aged longer: a cow will pay vi×av_i \times a for a treat of age aa. Given the values viv_i of each of the treats lined up in order of the index ii in their box, what is the greatest value FJ can receive for them if he orders their sale optimally?

The first treat is sold on day 11 and has age a=1a=1. Each subsequent day increases the age by 11.

Input Format

Line 11: A single integer, NN.

Lines 2N+12 \sim N+1: Line i+1i+1 contains the value of treat viv_i.

Output Format

Line 11: The maximum revenue FJ can achieve by selling the treats.

5
1
3
1
5
2
43

Hint

Explanation of the sample:

Five treats. On the first day FJ can sell either treat #1\#1 (value 11) or treat #5\#5 (value 22).

FJ sells the treats (values 1,3,1,5,21, 3, 1, 5, 2) in the following order of indices: 1,5,2,3,41, 5, 2, 3, 4, making $1 \times 1 + 2 \times 2 + 3 \times 3 + 4 \times 1 + 5 \times 5 = 43$.