Excursion
Time limit7sMemory limit2048 MB
Given a tree with a value on each node, find the maximum sum of values along a simple path that includes at least one node.
- Level
Medium4 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Jimmy goes on an excursion in the country of Treenidad and Treebago. The people there are so obsessed with trees that they modeled their country after them. As a careful planner, Jimmy wants to know in advance which cities he should visit to maximize the total appeal of his excursion. The appeal of a city is an integer that may be negative. Jimmy went through a lot of trouble to get his visa, so he wants to visit at least one city. He can start the excursion from any city. When visiting the country, he must not visit the same city twice.
Input
The first line contains an integer , the number of cities in Treenidad and Treebago. Then lines follow. The first of these describes the root of the tree. Each of the following lines contains two integers, and , which describe the properties of a node in the tree.
- is the value at that node, with .
- is the number of children of the node, with .
After that, lines follow, each recursively defining a child tree. The height of the tree is guaranteed to be at most .
Output
Print the maximum appeal Jimmy can gather on his excursion.