Points
InterviewTime limit3sMemory limit128 MB
Pick a subset of targets in a row to maximize the total where each picked target scores based on how many neighbors are also picked.
- Level
Medium5 of 10
- Topics
- Dynamic programming
- Solved
- No attempts yet
Problem
John likes amusement parks. He goes every weekend and plays a different game. This weekend he found a hard one, a target shooting game.
The targets stand in a row along a straight line, numbered from to from right to left. John picks the targets he wants to shoot, and every target he picks scores points. The score of target depends on how many of its neighbors, target and target , John picked as well.
- points if he picked neither neighbor
- points if he picked exactly one neighbor
- points if he picked both neighbors
Target and target have a single neighbor each, so they never have two picked neighbors. A target John does not pick scores nothing. Find the largest total score John can reach.
Input
The first line contains the number of targets . ()
Each of the next lines contains the values , , and of target , separated by spaces, in order from to . ()
Output
Print the maximum number of points John can win on a single line.
Hint
The input is always well formed, and the file ends right after the last target. One input holds exactly one set of targets. Print the result to standard output from the beginning of a line.