Happy Cow
InterviewTime limit2sMemory limit512 MB
Remove portions from either end of a row over N days; on day d a portion with value H gives H times d. Maximize total happiness.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Intervals
- Solved
- No attempts yet
Problem
Minho keeps one cow that he cares about. The cow's happiness is his own happiness, so he spent everything he had on portions of the tastiest feed.
To keep the feed tidy Minho put the portions in order into a warehouse that is narrow but very long, and he calls them feed 1, feed 2, feed 3, ..., feed counting from the left. Each portion can make the cow feel a different amount of happiness. Eating feed 1 may give the cow 20 happiness while feed 2 gives 10. Call that number the happiness value of the portion.
Feed ripens as days pass and tastes better. If the cow eats a portion with happiness value on day , the cow feels happiness. The day of the purchase is day 1 and the next day is day 2.
Feed aged for a million years would be bliss for the cow, but Minho cannot wait that long. He decided to give the cow one portion a day, from the day he bought the feed through day . The warehouse is too narrow to pull a portion out of the middle, so he can only take one from the left end or from the right end. That is, if portions through () are left, Minho must pick feed or feed . He cannot give any portion other than or .
Minho wants the cow to be as happy as possible. Find the maximum total happiness the cow feels when all portions are given over days.
Input
The first line contains the number of portions ().
The second line contains the happiness values (), separated by spaces.
Output
Print the maximum total happiness the cow feels when all portions are given over days.