#K48267. Valid Transport Sequence

    ID: 28383 Type: Default 1000ms 256MiB

Valid Transport Sequence

Valid Transport Sequence

You are given a sequence of transportation events. Each event is represented by an action and a rider identifier. The action is either board or alight. You need to determine whether this sequence of events is valid.

A sequence is considered valid if it satisfies the following conditions:

  • If an event is board, then the rider should not already be on board. In other words, if a rider attempts to board again without alighting first, the sequence is invalid.
  • If an event is alight, then the rider must already be on board. If a rider alights without having boarded, the sequence is invalid.
  • Note that it is allowed for riders to remain on board at the end of the sequence; they do not necessarily need to alight.

In mathematical terms, let \( S \) be the set of riders currently on board. For each event \( (action, id) \):

  • If \( action = \texttt{board} \), then the sequence is invalid if \( id \in S \) (i.e. \( id \) already in \( S \)). Otherwise, add \( id \) to \( S \).
  • If \( action = \texttt{alight} \), then the sequence is invalid if \( id \notin S \) (i.e. \( id \) not in \( S \)). Otherwise, remove \( id \) from \( S \).

Your task is to write a program that reads the sequence of events from standard input and prints VALID if the sequence is valid and INVALID otherwise.

inputFormat

The input is read from standard input and is formatted as follows:

  1. The first line contains a single integer \(n\) representing the number of events.
  2. The following \(n\) lines each contain a string and an integer separated by a space. The string is either board or alight indicating the action, and the integer is the rider's ID.

For example:

6
board 1
board 2
alight 1
board 1
alight 2
alight 1

outputFormat

Output a single line to standard output:

  • VALID if the sequence of events is valid.
  • INVALID otherwise.
## sample
6
board 1
board 2
alight 1
board 1
alight 2
alight 1
VALID