cswork / OAOpen practice workspace

NetflixOA #1

Command Undo Data Structure

Estimated EasyJudge-ready

Topics (AI-labelled)IteratorsStacks & Heaps

The state starts at 0. ADD x adds x, MUL x multiplies by x, and UNDO undoes the most recent change command that has not yet been undone; PRINT outputs the current state. PRINT never enters the change history, and there is no REDO.

Input

The first line contains q, followed by q lines of commands. This site uses 1≤q≤100000 and −10⁹≤x≤10⁹. Whenever UNDO runs, there is guaranteed to be a change that has not been undone, and all intermediate states fit in the signed64 range.

Output

On the first line output the total number of PRINT commands p; then output p lines with the state at each PRINT in order. If there is no PRINT, output only 0.

Example 1

Input:

4
ADD 5
MUL 3
UNDO
PRINT

Output:

1
5

Example 2

Input:

5
ADD 7
MUL 0
PRINT
UNDO
PRINT

Output:

2
0
7

Example 3

Input:

8
ADD 2
PRINT
ADD 4
UNDO
MUL -3
PRINT
UNDO
PRINT

Output:

3
2
-6
2

Example notes

Example 1: 5 times 3 gives 15; after undoing the multiplication, 5 is output. Example 2: multiplying by zero outputs 0, and undo can still restore 7; undo cannot be implemented by division. Example 3: 2, −6, 2 are output in order; PRINT does not take up an undo record, and after the add of 4 is undone, the new multiplication runs on state 2.

Editorial

Loading solution…