Spellcasting

Time limit1sMemory limit128 MB

Summary
Given element costs, power rates, and a support tree, find the minimum time for the spell's total power to reach the target, given starting energy and continuous accumulation.
Level

Hard9 of 10

Topics
Math, Greedy, Tree, Binary search
Solved
No attempts yet

Problem

Casting a spell is a continuous process. You begin with a certain amount of energy (measured in mana), which can be spent to summon elements into the spell. Each element can be summoned in any quantity instantaneously by spending energy, at which point it immediately becomes part of the spell and provides power (measured in mana per second) from then on. This power accumulates energy over time, which can in turn be used to summon additional elements. We continue this process until the total power of the spell reaches the required level.

There is one twist that experienced spellcasters exploit to cast more efficiently. Each element can have at most one parent element, which supports its summoning: if the parent element is already present, the child costs half as much energy as usual. For example, if element AA supports element BB and element CC, we could summon 1 unit of element AA at full cost first, then summon 0.5 unit of element BB and 0.5 unit of element CC at half cost. If we then summoned 0.5 more unit of element CC, that portion would again cost full energy, because the 1 unit of element AA is already fully supporting other elements. In other words, one unit of a parent element provides at most 1 unit of half-cost support to its children in total (shared across all of its children). All elements contribute their full power to the spell; supporting does not affect power output in any way, nor does it consume an element.

Given an initial amount of energy, a target amount of power, and a description of the spell elements available for summoning, determine how to cast a spell that reaches the target power in the minimum amount of time.

Input

The input consists of multiple test cases. Each test case begins with a line containing three space-separated integers NN, EE, and PP: the number of elements, the starting energy (in mana), and the target power (in mana per second). This is followed by NN lines, the ii-th of which describes element ii by three space-separated integers eie_i, pip_i, and parenti\mathrm{parent}_i: the energy cost to summon it, its power output, and the index of its parent element (1-indexed; parenti=0\mathrm{parent}_i = 0 if element ii has no parent).

Constraints:

  • 1≤N≤10001 \le N \le 1000, 1≤E≤1091 \le E \le 10^9, 1≤P≤1091 \le P \le 10^9.
  • 1≤ei≤1091 \le e_i \le 10^9, 0≤pi≤1090 \le p_i \le 10^9, and 0≤parenti≤N0 \le \mathrm{parent}_i \le N for all ii.
  • At least one pip_i is positive.
  • No element is its own ancestor; that is, no element can support itself, directly or indirectly.

The input is terminated by a case with N=E=P=0N = E = P = 0, which should not be processed.

Output

For each test case, print a single line containing a single integer: the minimum number of seconds required to reach the target power, rounded up to the nearest integer.

Examples3

  1. Example 1

    Input
    1 1 1000000
    200 100 0
    2 1 1000000
    200 100 0
    2 1 1
    2 1 1000000
    200 100 2
    2 1 0
    0 0 0
    
    Expected output
    30
    29
    14
    
  2. Example 2

    Input
    1 5 100
    3 4 0
    0 0 0
    
    Expected output
    3
    
  3. Example 3

    Input
    3 100 1000
    10 5 0
    2 3 0
    7 1 0
    0 0 0
    
    Expected output
    2