Committee
InterviewTime limit1sMemory limit1024 MB
Given a rooted tree with one node per person and a value per node, pick a nonempty connected subtree maximizing the sum of its node values.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
The Japanese Olympiad in Informatics Committee is an organization with a very strict hierarchy. There is exactly one chairperson, and every person other than the chairperson has exactly one supervisor. To preserve secrecy, each person in the organization knows only the faces of those they deal with directly, namely their direct supervisor and their direct subordinates. Electronic or public means of communication are not allowed, so two people who do not know each other must communicate through people who know both of them. Every person in the committee also has a fixed motivation value. Some people may have a negative motivation value.
A top-secret project is now being launched within the Japanese Olympiad in Informatics Committee, and at least one person must be chosen. Whether the project succeeds is thought to depend not on the number of chosen people but on the sum of their motivation values. Because the project is top-secret, any two people inside the project must be able to communicate without going through anyone outside the project.
Given each person's supervisor and motivation value as input, write a program that answers the maximum possible sum of motivation values over all valid choices.
Input
The first line of the input contains one integer (), the number of people in the Japanese Olympiad in Informatics Committee.
The next lines describe each person's supervisor and motivation value. Line () contains two integers and (, ) separated by a space. Here person 's supervisor is person and person 's motivation value is . When is , person is the chairperson. Since , every person's supervisor has a smaller number than that person.
Output
Write the output to standard output. Print a single integer, the maximum possible sum of motivation values.