Under construction

Still being built. Things may change

Vasya

Time limit2sMemory limit1024 MB

Summary
Check whether a recorded rooftop walk is valid, simulating jumps and moves, and report the final building or the first failing action.
Level

Medium4 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Statement

The city of Flatburg is celebrating. At last the city has its own King Kong, named Vasya. In his first week in Flatburg, Vasya chose one of the central streets, where all the buildings stand on one straight line. He likes best to walk across the rooftops along the street.

Vasya can move from the roof of a building of height h1h_1 to an adjacent building of height h2h_2 that is not taller, if 0≤h1−h2≤d0 \le h_1 - h_2 \le d.

Vasya always carries a huge bamboo pole of length ll meters. With the pole, he can climb onto an adjacent building whose height h2h_2 exceeds the height h1h_1 of his current building by at most ll. After that, a piece of the pole of length h2−h1h_2 - h_1 always breaks off.

Sometimes Vasya jumps on rooftops. If he jumps on the roof of building ii, its height decreases by aia_i, and the height of every other building increases by bib_i. If a building is trampled into the ground (its height becomes non-positive), Vasya will not jump on it anymore, since that is not interesting.

Recently the residents of Flatburg gave Vasya a birthday story about one of his walks, from the roof of the first building to the roof of the nn-th building. Vasya doubts the story is factual. He asks you to check whether he could have walked from the roof of building 1 to the roof of building nn exactly as the story describes.

Input

The first line contains integers nn, dd, and ll, where 1≤n≤20001 \le n \le 2000 and 0≤d,l≤1090 \le d, l \le 10^9. Each of the next nn lines contains three integers hih_i, aia_i, and bib_i: the initial height of building ii, the amount its height drops when Vasya jumps on it, and the amount it rises when Vasya jumps on another building. It is given that 0<hi,ai,bi≤1090 < h_i, a_i, b_i \le 10^9.

The remaining lines of the input contain the story. Each action in the story is written in one of these forms:

  • 'Vasya jumps on building ii' describes a jump on building ii.
  • 'Vasya moves from building ii to building jj' describes a move from building ii to building jj. It is guaranteed that ∣i−j∣=1|i - j| = 1.

The story is at most 100 kilobytes. Each line has at most 1000 characters. The story uses only Latin letters and digits. Any two adjacent words are separated by exactly one space or a line break.

Output

If the story contains an error, meaning Vasya cannot perform some described action, print 'ERROR' on the first line. On the second line, print ii, the number of the first action Vasya could not perform.

Otherwise, print the number of the building where Vasya is at the end of the story.

Examples3

  1. Example 1

    Input
    2 1 1
    1 1 1
    5 5 5
    Vasya moves from building 1
    to building 2
    
    Expected output
    ERROR
    1
    
  2. Example 2

    Input
    3 1 10
    11 3 4
    21 7 2
    5 2 2
    That wonderful summer morning Vasya decided to have a walk
    After drinking a cup of tea Vasya moves from building 1
    to building 2 then Vasya jumps on building 2 then
    he jumps on building 2 Vasya jumps on building 2
    And he jumps and jumps and jumps
    Vasya moves from building 2 to building 3
    
    Expected output
    ERROR
    4
    
  3. Example 3

    Input
    3 1 10
    11 3 4
    21 7 1
    5 2 2
    That wonderful summer morning Vasya decided to have a walk
    After drinking a cup of tea Vasya moves from building 1
    to building 2 then Vasya jumps on building 2 then
    he jumps on building 2 Vasya jumps on building 2
    And he jumps and jumps and jumps
    Vasya moves from building 2 to building 3
    
    Expected output
    3