Under construction

Still being built. Things may change

Stack 2

Interview

Time limit2sMemory limit1024 MB

Summary
Process N stack commands that push integers, pop or peek the top value, report the size, and check emptiness, printing -1 when the stack is empty.
Level

Easy2 of 10

Topics
Stack, Implementation
Solved
No attempts yet

Problem

Implement a stack that stores integers, then write a program that processes the commands given as input. There are five commands.

  1. 1 X: Push the integer X onto the stack. (1 ≤ X ≤ 100,000)
  2. 2: If the stack has an integer, remove the top integer and print it. Otherwise, print -1.
  3. 3: Print the number of integers in the stack.
  4. 4: Print 1 if the stack is empty, otherwise print 0.
  5. 5: If the stack has an integer, print the top integer. Otherwise, print -1.

Input

The first line contains the number of commands N (1 ≤ N ≤ 1,000,000). The next N lines each contain one command. At least one command asks for output.

Output

Each time a command that requires output is given, print its result on its own line.

Examples1

  1. Example 1

    Input
    9
    4
    1 3
    1 5
    3
    2
    5
    2
    2
    5
    
    Expected output
    1
    2
    5
    3
    3
    -1
    -1