Cow Pinball
InterviewTime limit1sMemory limit128 MB
Given a triangle of nail scores with R rows, find the maximum sum along a path from the top nail down to the last row, moving to one of the two adjacent nails below each step.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Array, Matrix, Recursion
- Solved
- No attempts yet
Problem
The cows are playing a game called pachinko. You drop a ball in at the top, and as it falls it strikes nails, veering a little to the left or right, until it comes out at the bottom.
This pachinko machine is special. The ball always strikes the single top nail of the rows of nails (), and then hits either the left or right nail just below it. From there it again falls to the left or right nail just below, and this repeats all the way down to the last row. The ball never veers too far — no more than half a nail — from the nail it just struck.
Scoring is unusual too: you earn points () for every nail you strike on the way down. The cows want to maximize their score. What is the highest score they can achieve?
Here is an example triangle and a good route; the nails marked with * form the path.
7 *7
3 8 *3 8
8 1 0 *8 1 0
2 7 4 4 2 *7 4 4
4 5 2 6 5 4 *5 2 6 5
In the example above, the route gives the highest sum, . Going from to and then to again is impossible: the in the third row is too far away.
Input
- Line 1: a single integer .
- Lines 2 to : line contains the space-separated scores of the -th row of the machine: .
Output
- Line 1: a single integer, the maximum achievable score.