All problems

Problem 129

Min Stack

Medium · 20 pts · Stack, Design

Problem

Simulate a stack that supports four operations, each in O(1) time: push x adds x on top, pop removes the top element, top reports the top element, and min reports the smallest value currently in the stack. Print the result of every top and min operation, each on its own line. The operations are always valid, so pop, top and min are never called on an empty stack. The trick is to store, alongside every element, the minimum of the stack at the moment it was pushed. Then the current minimum is always stored with the top element, even after pops.

Input

First line: integer Q. Next Q lines: one operation each: 'push x', 'pop', 'top' or 'min'.

Output

Print the answer of each top and min operation on its own line.

Constraints

1 <= Q <= 100000; -1000000000 <= x <= 1000000000; at least one top or min operation

Example input

6
push -2
push 0
push -3
min
pop
top

Example output

-3
0

Explanation

After push -2, push 0 and push -3, the stack is -2 0 -3 and min reports -3. After pop the stack is -2 0, so top reports 0. The output lines are -3 and 0.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Evaluate Reverse Polish Notation Next: Largest Rectangle in Histogram