Treats for the Cows
InterviewTime limit1sMemory limit1024 MB
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 yummy treats () 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 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 has value ().
- Cows pay more for treats that have aged longer: a cow will pay for a treat of age .
The first treat is sold on day 1 and has age . Each subsequent day increases the age by 1.
Given the values of the treats in order of index 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 . Starting from line 2 through line , line contains the value of treat .
Output
Print one line containing the maximum revenue FJ can achieve by selling the treats.