Dao and Dizzy's Date
Time limit1sMemory limit1024 MB
Walk on a line of N places for T minutes starting and ending at place 1, gaining h[j] whenever you move to place j, and maximize total happiness.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Math, Greedy, Prefix sum
- Solved
- No attempts yet
Problem
The new year has come to Bubble Hill in Crazy Park. To celebrate, Dao and Dizzy are going on a date and plan to look around Bubble Hill.
Bubble Hill consists of places connected in a straight line. There are roads linking pairs of places: for each integer from to , place and place are connected by a road.
Dao and Dizzy plan their date minute by minute. Each minute they choose one of the following three actions.
- If they are at place with , they take a road to place .
- If they are at place with , they take a road to place .
- They stay where they are.
Each minute, Dao and Dizzy gain happiness depending on the place they were at one minute earlier and the place they are at now. If , they gain happiness. The value may be negative, which means they lose happiness. If , their happiness does not change.
Only minutes remain for the date, so they want to start from the village, make their happiness as large as possible, and come back. That is, the starting and ending place must always be village 1, where Dao and Dizzy live. Find the happiness Dao and Dizzy will gain.
Input
The first line gives two integers and . (, )
The second line gives integers separated by spaces; the -th number is . (, )
Output
Print the maximum happiness Dao and Dizzy can gain on this date.