Vasya
Time limit2sMemory limit1024 MB
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 to an adjacent building of height that is not taller, if .
Vasya always carries a huge bamboo pole of length meters. With the pole, he can climb onto an adjacent building whose height exceeds the height of his current building by at most . After that, a piece of the pole of length always breaks off.
Sometimes Vasya jumps on rooftops. If he jumps on the roof of building , its height decreases by , and the height of every other building increases by . 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 -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 exactly as the story describes.
Input
The first line contains integers , , and , where and . Each of the next lines contains three integers , , and : the initial height of building , 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 .
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 ' describes a jump on building .
- 'Vasya moves from building to building ' describes a move from building to building . It is guaranteed that .
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 , 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.