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.
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.
−1
means that the topmost book is removed.−2
means that the robotic arm is used.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.
For each −1
in the input, output a single line with the number of the book that was removed in that step.
3
12345
23456
34567
45678
-2
-1
98765
-2
-1
-1
-2
-1
0
23456
45678
34567
12345