tree dynamic programming