Excursion

시간 제한7초메모리 제한2048 MB

요약
음수 값이 가능한 루트 트리에서 한 개 이상의 노드를 방문하는 단순 경로 가중치의 최댓값을 구한다.
난이도

보통10점 중 6점

유형
트리, DFS, 동적 계획법, 재귀
정답자
아직 제출이 없습니다

문제

Jimmy goes on an excursion in the country of Treenidad and Treebago. People there are obsessed with trees so much, they modeled their country after them. Being a careful planner, Jimmy wants to know in advance which cities should be visited to maximize the total appeal of his excursion. The appeal of a city is defined by a not necessarily positive integer. Since he went through a lot of hassle to get his visa, he wants to visit at least one city. Jimmy's excursion can start from any city. His only requirement when visiting the country is that he mustn't visit the same city twice.

입력

The first line in the input contains an integer 1≤n≤1061\leq n \leq 10^6, the number of cities in Treenidad and Treebago.\\ After that, nn lines follow, the first of which 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 represents the value at that node, with −231≤V<231-2^{31} \leq V < 2^{31}.
  • CC represents the number of children of the node, with 0≤C<1060 \leq C < 10^6.

After that, CC lines follow, each recursively defining the child trees. It is guaranteed that the height of the tree is less than or equal to 990990.

출력

The maximum appeal Jimmy can gather in his excursion.

예제2

  1. 예제 1

    입력
    5
    5 2
    2 2
    1 0
    10 0
    -1 0
    
    예상 출력
    17
    
  2. 예제 2

    입력
    7
    1 3
    3 2
    5 0
    6 0
    2 1
    3 0
    4 0
    
    예상 출력
    15