#P4732. Multilevel Undo Simulation

    ID: 17976 Type: Default 1000ms 256MiB

Multilevel Undo Simulation

Multilevel Undo Simulation

Byteasar is developing a revolutionary text editor with a novel multilevel undo feature. There are two types of operations:

  • Editing operation E s: sets the editor state to an integer s. Each editing operation is considered to be of level \(0\).
  • Undo operation U i: an undo operation of level \(i\) (where \(i \ge 1\)) undoes the most recent active operation whose level is at most \(i-1\). In other words, an undo operation of level \(1\) undoes the last active editing operation, while an undo operation of level \(2\) can undo an editing operation or an undo operation of level \(1\), and so on.

When an operation is undone its state is changed to undone (inactive). However, if an undone operation is an undo operation, then the operation it previously undid will be restored (its state toggled to active). This toggling can cascade: whenever the state of an undo operation changes, the operation it previously affected also has its state toggled, and the process continues until an editing operation is reached.

Initially, the editor state is \(0\). The effective editor state at any moment is the state produced by the most recent active editing operation.

Your task is to simulate these operations and output the final editor state.

Note on Undo Process (in \(\LaTeX\) format):
If an undo operation \(U_i\) is performed, let \(X_1\) be the most recent active operation with level at most \(i-1\). Then change the state of \(X_1\) to undone. If \(X_1\) is an undo operation that had previously undone some operation \(X_2\), restore \(X_2\) (i.e. change its state to active). This cascading toggling continues until an editing operation is reached.

inputFormat

The first line contains an integer \(n\) (the number of operations). Each of the following \(n\) lines contains an operation in one of the following forms: E s or U i. For an editing operation, \(s\) is an integer representing the new editor state. For an undo operation, \(i\) (\(\ge 1\)) represents its level.

outputFormat

Output a single integer, the final editor state after processing all operations.

sample

11
E 1
E 2
E 5
U 1
U 1
U 3
E 4
U 2
U 1
U 1
E 1
1