MINSTACK - Smallest on the Stack


Every Christmas the good old man can go to every house in the world and leave gifts for the children who have been good throughout the year, but this is only possible because of his magic gift bag. It would be impossible for Santa to carry all the presents in his bag, the volume and weight of them all makes this obviously unfeasible. What actually happens is that your bag is a kind of magical portal to your gift factory at the North Pole. Where the presents are stacked by their elves and Noel always takes the gift from the top of that pile when he accesses his magical bag.

Gifts have a numerical measure of the degree of fun they can provide children, and Santa is always concerned with the least fun gift he will deliver throughout the night because he does not want any child to feel bad about it. you receive. However, this can not be done in advance because throughout the night as the good old man takes gifts from the pile to deliver, others are still being made and placed on the pile. So the most he can know is the value of the least fun present on the stack up to that point.

Your task is, given the sequence of operations done on the stack of gifts, answer Santa's queries about the value of the least entertaining gift on the stack thus far.

Input

The first line of the input contains an integer N (1 ≤ N ≤ 106) corresponding to the number of operations performed on the present stack. The operations can be of three types: "PUSH V" where V (1 ≤ V ≤ 109) is an integer representing the degree of fun of the present being placed on the stack; "POP" which represents that Santa Claus is taking a gift from the cell to deliver and "MIN" representing a Noel query to know the smallest gift value in the stack.

Output

The output consists of a line containing an integer with the smallest present value in the stack for queries of type "MIN" or "EMPTY" for "MIN" and "POP" operations when the stack is empty.

Example

Input:
 11
PUSH 5
PUSH 7
PUSH 3
PUSH 8
PUSH 10
MIN
POP
POP
MIN
POP
MIN Output:

3

3

5

Example

Input:
 9
PUSH 100
PUSH 50
MIN
PUSH 45
MIN
POP
MIN
POP
MIN Output:

50

45

50

100


hide comments
nadstratosfer: 2019-03-19 08:36:55

Adarsh, this problem features large input so correct complexity might not be enough if the code style is poor, resulting in high constant factor, or IO method slow. Try various input methods at INTEST and see if you can get anywhere close to the Java top there: https://www.spoj.com/ranks/INTEST/lang=JAVA . This should help, albeit psetter is correct that it's absolutely possible to get AC without fast IO, or with a slow language.

adarsh18213: 2019-03-03 12:32:45

I'm new to SPOJ.
I'm getting Time Exceed Error, but my solution is a Linear Time solution, and I suppose the ideal solution for this problem is Linear Time, then how could I get a such an error? I'm coding in JAVA.
Thanks

aadarsh45: 2019-02-18 18:53:42

Be carefully about SIGSEGV Runtime error
I got many times SIGSEGV error

ayushgupta1997: 2019-02-11 20:19:56

Use ios_base::sync_with_stdio(false);cin.tie(0); for C++, atleast worked in my case :)

zarif_2002: 2019-02-05 05:59:57

no way, scanf and printf makes AC and cin and cout makes TLE.

Author: There's a way ;)

Last edit: 2019-02-05 13:51:41
Nishant Gupta: 2019-01-10 13:36:52

@Robert : +1
Even using cout was giving TLE, had to replace it with printf to get AC.

y17prashant: 2019-01-08 19:25:32

Simple stack implemenation . Just go for O(1) search of minimum element , to avoid TLE

Francisco Elio Parente Arcos Filho [UEA]: 2018-12-30 23:54:08

Its absolutely possible achieve the accepted without fast io

Robert Gawron: 2018-12-29 13:50:11

I had to replace cin by scanf to get acceptance due to too long processing time.

julkas: 2018-12-27 12:15:40

Happy New Year for all SPOJ users!


Added by:Francisco Elio Parente Arcos Filho [UEA]
Date:2018-12-24
Time limit:1s-1.200s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All