Under construction

Still being built. Things may change

Treats for the Cows

Interview

Time limit1sMemory limit1024 MB

Summary
Given N treat values in a box open at both ends, find the maximum revenue when each treat is sold from either end with its value times its age.
Level

Medium5 of 10

Topics
Dynamic programming, Two pointers
Solved
No attempts yet

Problem

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

The treats are interesting for many reasons:

  • The treats are numbered 1 to NN 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.
  • 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 v(i)v(i) (1≤v(i)≤10001 \le v(i) \le 1000).
  • Cows pay more for treats that have aged longer: a cow will pay v(i)×av(i) \times a for a treat of age aa.

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

Given the values v(i)v(i) of the treats in order of index ii in the box, what is the greatest value FJ can receive for them if he orders their sale optimally?

Input

The first line contains a single integer NN. Starting from line 2 through line N+1N+1, line i+1i+1 contains the value v(i)v(i) of treat ii.

Output

Print one line containing the maximum revenue FJ can achieve by selling the treats.

Examples1

  1. Example 1

    Input
    5
    1
    3
    1
    5
    2
    
    Expected output
    43