A spillway is built as a triangular array of terraces. Row 0 has one terrace; row i has i + 1 terraces. Water starts on the single terrace of row 0. From the terrace at position j of row i it flows to the terrace at position j or position j + 1 of row i + 1 (the two terraces directly beneath it). Each terrace holds an integer weight that is added to the running total as the water passes over it, including the apex and the final terrace reached in the bottom row.
Determine the minimum possible total over all descents from the apex to any terrace in the bottom row.
Line 1: an integer T, the number of rows.
Next T lines: line i (0-indexed) contains i + 1 integers, the terrace weights of that row.
A single integer: the minimum total weight of a descent from the apex to the bottom row.
Example 1
Input
4 2 3 4 6 5 7 4 1 8 3
Expected
11
Explanation
The descent apex 2 -> 3 -> 5 -> 1 sums to 2+3+5+1 = 11, which is the smallest total any top-to-bottom descent can reach.
Example 2
Input
1 5
Expected
5
Explanation
A single terrace means the only descent has total 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →