#C4696. Elevator Operation Simulator

    ID: 48262 Type: Default 1000ms 256MiB

Elevator Operation Simulator

Elevator Operation Simulator

You are given a building with n floors, numbered from 1 to n. An elevator starts at floor 1. You will receive m commands to move the elevator. Each command is a string in the format UP X or DOWN X, where X is an integer indicating how many floors to move.

When moving up, the elevator cannot exceed floor n. Similarly, when moving down, the elevator cannot go below floor 1. Formally, after an UP X command, the new floor is given by:

\( \min(n, \text{current_floor} + X) \)

And after a DOWN X command, the new floor is given by:

\( \max(1, \text{current_floor} - X) \)

Your task is to compute the final floor of the elevator after executing all the commands.

inputFormat

The input is read from standard input (stdin) and is in the following format:

  • The first line contains two integers n and m separated by a space, where 1 ≤ n ≤ 109 and 0 ≤ m ≤ 105.
  • The following m lines each contain a command in the format UP X or DOWN X, where X is a positive integer.

outputFormat

Output to standard output (stdout) a single integer representing the final floor of the elevator.

## sample
10 3
UP 3
DOWN 1
UP 5
8