Under construction

Still being built. Things may change

Evacuation

Time limit3sMemory limit1024 MB

Summary
Each room has one person who must reach a leaf exit before the detonator on each corridor fires. Find the maximum number who escape.
Level

Hard9 of 10

Topics
Tree, Dynamic programming, Greedy, Sorting
Solved
No attempts yet

Statement

A terrible thing has happened. Doctor Octavius has taken over an industrial laboratory. The laboratory consists of nn rooms, some of which are connected by corridors. There are n−1n-1 corridors, and every room can be reached from every other room. In other words, the laboratory is a tree with nn vertices. Each corridor has a travel time lil_i, which is the time needed to go from one end of the corridor to the other. Corridors can be walked in both directions.

Each room holds exactly one person. Doctor Octavius plans to kill all of them, but he has given them a chance to survive. The exits are located at the leaves of this tree. A leaf is a vertex with degree 00 or 11. A person who reaches an exit escapes from Doctor Octavius's tentacles. Each corridor has a detonator set by the villain. Each detonator has a time tit_i at which it explodes. Once a detonator explodes, nobody can move through its corridor anymore, and anyone inside that corridor at that moment dies. If a person reaches the far end of the corridor at the moment of the explosion, they survive.

Spider-Man cannot reach these people in time, so they must save themselves. Find the maximum number of people who can escape.

Input

The first line contains one integer nn (1≤n≤1000001 \le n \le 100000), the number of rooms. Each of the next n−1n-1 lines contains four integers aia_i, bib_i, lil_i, and tit_i (1≤ai,bi≤n1 \le a_i, b_i \le n; 1≤li≤100001 \le l_i \le 10000; 1≤ti≤1091 \le t_i \le 10^9). These are the two rooms connected by the corridor, the time needed to walk through it, and the moment its detonator explodes.

It is guaranteed that the laboratory is a tree.

Output

Print one integer, the maximum number of people who can escape.

Hint

In the first example, the person in room 5 runs to room 4, and the person in room 3 runs to the same room. That person arrives exactly at the moment of the explosion, but still survives.

In the second example, the person in room 4 runs to room 2, and the person in room 1 runs to room 5. The person in room 3 does not make it out.

Examples2

  1. Example 1

    Input
    5
    3 5 2 3
    1 3 3 2
    4 5 2 4
    2 5 1 2
    
    Expected output
    5
    
  2. Example 2

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