Max Heap
InterviewTime limit1sMemory limit256 MB
Process N insert and pop-max operations on an initially empty max heap, printing the maximum or 0 when empty.
- Level
Easy3 of 10
- Topics
- Heap
- Solved
- No attempts yet
Problem
The max heap is a widely used data structure. Write a program that supports the following two operations with a max heap.
- Insert a natural number into the array.
- Print the largest value in the array and remove that value from the array.
The program starts with an empty array.
Input
The first line contains the number of operations (). Each of the next lines contains one integer describing an operation. If is a natural number, insert into the array. If is 0, print the largest value in the array and remove that value from the array. Every natural number in the input is smaller than .
Output
Print one answer per line, as many times as 0 appears in the input. If the array is empty when the largest value is requested, print 0.