Line of Bentham
Time limit1sMemory limit256 MB
Replace some people in a line with agents so the total happiness, where each person sums likes over the three ahead, is maximized.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
N people stand in a line facing forward. The front person is number 1 and the back person is number N.
Each person distinguishes at most the 3 people directly ahead. For person and person , the integer records how much likes and lies from to . The happiness of person is . When , define . The total happiness is .
Any positions in the line may be replaced with dispatched agents. An agent has happiness and does not affect the happiness of people behind. There is no limit on the number of agents.
Compute the maximum total happiness obtainable from the N people.
Input
The first line gives the number of people with .
The next lines describe each person. The line for person gives three integers , and .
When , the value is given as . All other are integers from to .
Output
Print the maximum total happiness obtainable after replacing some people with agents.
Hint
In the public sample input, keeping everyone gives happiness values , , and for a total of . Replacing persons and with agents gives , and no replacement reaches a larger total.