#K85132. Book Stack Operations
Book Stack Operations
Book Stack Operations
You are given a series of operations to perform on a stack of book IDs. The stack supports the following operations:
push x
: Push the book with ID x onto the stack.pop
: Remove the book from the top of the stack.max
: Print the maximum book ID present in the stack at that moment.
It is guaranteed that pop
and max
operations will only be called when the stack is non-empty.
Your task is to implement these operations efficiently. For each max
operation, output the current maximum value in the stack on a new line.
inputFormat
The input consists of an integer N on the first line, representing the total number of operations. Each of the following N lines contains a single operation in one of the following formats:
push x
where x is an integer.pop
max
All inputs are read from stdin
.
outputFormat
For each max
operation, output the maximum book ID currently in the stack. Each result should be printed on a separate line to stdout
.
6
push 1
push 2
max
push 3
pop
max
2
2
</p>