cho.sh
Notes
Loading...

Adventure Game

Time limit

1s

Memory limit

128 MB

Problem

A magical maze has rooms numbered from 1 to n. Each room may have doors to other rooms, and each door leads to the room with the written number. A room is either empty, contains a leprechaun who can refill the adventurer's gold, or contains a troll who charges an entrance fee.

When the adventurer enters a leprechaun room, if their current gold is less than that room's amount, it is raised to that amount. If they already have at least that much gold, it is unchanged. To enter a troll room, the adventurer must pay that room's amount; if they cannot pay, they cannot enter. This rule also applies when the adventurer first enters room 1.

The adventurer starts with 0 gold. Determine whether they can start from room 1 and reach room n.

Input

The input contains several mazes. The first line of each maze contains the number of rooms n (1 <= n <= 1000).

The next n lines describe rooms 1 through n in order. Each line contains a character for the room type (E, L, or T), an amount, and the list of room numbers reachable through doors from that room. E means an empty room and its amount is 0. L means a leprechaun room, and T means a troll room; for these rooms, the amount is a positive integer at most 500. Each door list ends with 0.

If 0 is given as the number of rooms, the input ends.

Output

For each maze, print one line. Print Yes if room n can be reached from room 1; otherwise print No.