This page is still under construction.

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

Juice

Interview

Time limit2sMemory limit128 MB

Summary
Given a rooted tree with cord capacities and house demands, choose which houses to power so that flow through each cord stays within capacity and the count is maximized.
Level

Medium6 of 10

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

Problem

In a favela in Rio de Janeiro, a light finally flickered on. After months of careful work, the residents connected a generator to thousands of extension cords, and the slum lit up with millions of bright lights.

However, the extension cords could not carry enough power to meet the energy demand of every house in the slum. So, before switching on the generator, the engineers had to choose carefully which houses to power and which to leave dark. Their goal was to power as many houses as possible, given each house's energy demand and the capacity of the extension cords.

The generator and the houses are modeled as nodes, and the extension cords as edges connecting them. Each node draws power from exactly one other node, so the whole network forms a tree rooted at the generator. Every node except the generator has a non-negative energy demand. The generator can supply far more power than the total capacity of the cords attached to it, so treat it as an infinite source.

Power reaches a house by traveling along the cords out from the generator. For every cord, the total power flowing through it — that is, the sum of the demands of all powered houses located below that cord — must not exceed the cord's capacity. A house counts as powered only when its full demand is delivered.

Determine the maximum number of houses whose energy demands can be met.

Input

The first line contains a single integer nn (0≤n≤10000 \le n \le 1000), the number of houses.

Each of the next nn lines describes one house ii (for i=1,2,…,ni = 1, 2, \dots, n) with three integers pi ri cip_i\ r_i\ c_i:

  • pip_i (0≤pi≤n0 \le p_i \le n) is the parent node of house ii;
  • rir_i (0≤ri≤1000 \le r_i \le 100) is the energy demand of house ii;
  • cic_i (1≤ci≤1001 \le c_i \le 100) is the capacity of the extension cord connecting house ii to node pip_i.

The generator has index 00.

Output

Print a single integer: the maximum number of houses whose energy demands can be met.

Examples1

  1. Example 1

    Input
    3
    0 3 2
    0 100 100
    1 1 1
    
    Expected output
    2