This is a dynamic programming problem, where lower-level sub-solutions are combined into higher-level sub-solutions until the final solution is reached at the highest level. Consider the sample triangle. If your path reaches the 2 on the third row of the triangle, you will certainly move left to the 8 on the fourth row. If your path reaches the 4 on the third row of the triangle, you will certainly move right to the 9 on the fourth row. And if your path reaches the 6 on the third row of the triangle, you will certainly move left to the 9 on the fourth row. Thus, the sample four-row triangle has the same solution as the three-row triangle shown below:

3
7 4
10 13 15

Then apply the process moving up the tree until you reach the top row. Here is that idea reduced to code:

The step internal function calculates the pair-wise maximums of the last row in the inner map, then adds them to the next-to-last row in the outer map. The loop runs over the rows of the triangle in reverse order. reporting the accumulated sum at the top. The but-last function returns a list minus its last element. Here are two examples:

> (max-sum sample)
23
> (max-sum input18)
1074

Calculation of the path follows the same algorithm, but saves the path instead of the sum at each step:

That looks more complicated than it really is. In its first argument, each step receives a list-of-lists of selected path elements instead of a list of their sums, so it has to calculate and compare the sums, then combine the new path element with the winning list-of-lists. The fix-last-row function makes each element of the last row of the triangle into a singleton list so that its type is list-of-lists. Here are two examples:

In C++: read data from stdin to an stl::vector, check it’s triangular and compute what the row size is. Loop over vector filling in maximum subtree sizes, then we can retrieve the path fairly easily by scanning the data again from the top (no need to keep track of paths as we fill in table):