Tagy: Python 2 dequeue
Obtiažnosť: medium

Book stack

When someone returns a borrowed book into the library, the librarian just throws it on the top of a large stack.

From time to time, she picks up the topmost book from the stack and carries it over to the shelf where it belongs.

Finally, she has a robotic arm to help her. The arm is able to grab the top $K$ books in the stack (or the entire stack if there are less than $K$ books in it) and flip it upside down.

Simulate this process.

Vstup

The input contains up to $1\,000\,000$ lines.

The first line contains the integer $K \, (1 \leq K \leq 1\,000\,000)$.

Each of the following lines contains a single integer representing an action.

  • Each positive integer represents a book to be put onto the stack. (At some moments the book stack may contain multiple copies of the same book.)
  • The number −1 means that the topmost book is removed.
  • The number −2 means that the robotic arm is used.
  • The number 0 terminates the input.

No book number will exceed $10^9$. You will never be requested to remove a book from an empty stack.

Warning: The inputs are rather large. A good solution using iostream should still pass, but still we suggest to use standard I/O instead of streams.

Výstup

For each −1 in the input, output a single line with the number of the book that was removed in that step.

Príklad

Vstup

Výstup

3
12345
23456
34567
45678
-2
-1
98765
-2
-1
-1
-2
-1
0
23456
45678
34567
12345
Ak chceš riešiť túto úlohu, musíš sa najprv prihlásiť.