Dynamic search for max path sum