Under construction

Still being built. Things may change

Excursion

Time limit7sMemory limit2048 MB

Summary
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 1≤n≤1061\leq n \leq 10^6, the number of cities in Treenidad and Treebago. Then nn lines follow. The first of these describes the root of the tree. Each of the following lines contains two integers, VV and CC, which describe the properties of a node in the tree.

  • VV is the value at that node, with −231≤V<231-2^{31} \leq V < 2^{31}.
  • CC is the number of children of the node, with 0≤C<1060 \leq C < 10^6.

After that, CC lines follow, each recursively defining a child tree. The height of the tree is guaranteed to be at most 990990.

Output

Print the maximum appeal Jimmy can gather on his excursion.

Examples2

  1. Example 1

    Input
    5
    5 2
    2 2
    1 0
    10 0
    -1 0
    
    Expected output
    17
    
  2. Example 2

    Input
    7
    1 3
    3 2
    5 0
    6 0
    2 1
    3 0
    4 0
    
    Expected output
    15