#C1053. Stack Operations with Maximum Query
Stack Operations with Maximum Query
Stack Operations with Maximum Query
You are given a series of operations on a stack. The possible operations are:
Push x
: Push the integer x onto the stack.Pop
: Remove the top element from the stack. It is guaranteed that the stack is non-empty when this operation is performed.PrintMax
: Print the maximum element contained in the stack at that moment.
Your task is to process these operations in the given order. Every time a PrintMax
operation is encountered, output the current maximum element. Note that if the stack is empty when PrintMax
is encountered, no output should be produced for that command.
Formally, if we denote the stack as \(S\) and update it with each operation, when executing a PrintMax
operation you must output \(\max(S)\) if \(S \neq \emptyset\). The operations are to be handled in the sequence they are provided.
inputFormat
The input is given from stdin and is structured as follows:
- The first line contains an integer \(n\), the number of operations.
- The following \(n\) lines each contain a single operation in one of the following forms: "Push x", "Pop", or "PrintMax".
outputFormat
For each PrintMax
operation, print the maximum element in the stack on a new line on stdout. If a PrintMax
operation is executed when the stack is empty, nothing should be printed for that command.
5
Push 3
Push 5
PrintMax
Push 7
PrintMax
5
7
</p>