Think of your pyramid as a tree with a root at the top of the pyramid: I think you need a route with a maximum cost from the root to any of the leaf nodes (at the bottom of the pyramid). OK, this is actually not a tree, because a node can have two parents, in fact, you can go to node at level i
no more than two nodes at level i-1
.
In any case, I think you can calculate the route with the maximum cost using dynamic programming. Let me rewrite your data as a matrix:
7 4 8 1 8 9 2 4 6 7 4 6 7 4 9 4 9 7 3 8 8
and let the missing elements of the matrix be 0. Let us call this matrix v
(for values). Now you can build the matrix c
(for costs), where c(i,j)
is the maximum cost to reach the node tree at position (i,j)
. You can calculate it with this repetition:
c(i,j) = v(i,j) + max{ c(i-1,j-1), c(i-1,j) }
where c(h,k)
is 0 when you get to the position from the matrix. Essentially, we say that the maximum cost of switching to a node in position (i,j)
is the cost of the node itself plus the maximum between the costs of getting two possible parents at the i-1
level.
Here is an example c
for your example:
7 11 15 12 23 24 14 27 30 31 18 33 37 35 40 22 42 44 40 48 48
For example, take i=3, j=2
:
c(3,2) = v(3,2) + max{ c(2,1), c(2,2) } = 6 + max{ 23 , 24 } = 30
From c
you see that the most expensive traffic costs 48 (and you have two of them).