This page is still under construction.

Parts of this page are still being built. What you see may change.

Committee

Interview

Time limit1sMemory limit1024 MB

Summary
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 nn (n≤100,000n \le 100{,}000), the number of people in the Japanese Olympiad in Informatics Committee.

The next nn lines describe each person's supervisor and motivation value. Line i+1i+1 (1≤i≤n1 \le i \le n) contains two integers sis_i and aia_i (0≤si<i0 \le s_i < i, −100≤ai≤100-100 \le a_i \le 100) separated by a space. Here person ii's supervisor is person sis_i and person ii's motivation value is aia_i. When sis_i is 00, person ii is the chairperson. Since si<is_i < i, 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.

Examples1

  1. Example 1

    Input
    5
    0 10
    1 5
    2 -8
    1 -15
    4 3
    
    Expected output
    15