Div II, 500 points

Calculate moving backwards in a recursive tree.