#C4838. Simulate Lift Operations

    ID: 48420 Type: Default 1000ms 256MiB

Simulate Lift Operations

Simulate Lift Operations

You are given a building with N floors (numbered 0 to N-1) and a lift initially at floor 0. You will be given M commands to operate the lift. Each command is in one of the following formats:

  • U X: Move up X floors. The lift cannot go above floor N-1.
  • D X: Move down X floors. The lift cannot go below floor 0.
  • S X: Set the lift to floor X. If X is out of the allowed range, it will be clamped to the range [0, N-1].
  • W: Wait, do nothing.

After processing all commands, output the final floor of the lift.

The operations are defined mathematically as follows:

For a command U X:
\(\text{current_floor} = \min(\text{current_floor} + X, N - 1)\)

For a command D X:
\(\text{current_floor} = \max(\text{current_floor} - X, 0)\)

For a command S X:
\(\text{current_floor} = \min(\max(X, 0), N - 1)\)

inputFormat

The first line contains two integers N and M, representing the number of floors and the number of commands, respectively.

The following M lines each contain a command as described above.

outputFormat

Output a single integer — the final floor of the lift after executing all the commands.

## sample
5 6
U 2
D 1
S 4
W
D 3
U 1
2