Priority Queue

A priority queue is a data structure that maintains a set S of elements, each with an associated value (key), and supports the operations insert(S, k) to insert an element k into the set S, and extractMax(S) to remove and return the element of S with the largest key. Write a program to perform these operations on a priority queue S, managing a set of integers, which are also keys for the priority.

Input

Multiple operations to the priority queue S are given. Each operation is in the form "insert k", "extract", or "end" in a line, where k represents an integer element to be inserted to the priority queue. The input ends with "end" operation.

Output

For each "extract" operation, print the element extracted from the priority queue S in a line.

Constraints

- The number of operations ≤ 2,000,000
- 0 ≤ k ≤ 2,000,000,000

Sample Input 1
insert 8
insert 2
extract
insert 10
extract
insert 11
extract
extract
end

Sample Output 1
8
10
11
2

# Buggy version:
import sys

def insert(heap, key):
    heap.append(-1000000)
    heapIncreaseKey(heap, key)

def parent(i):
    return (i - 1) // 2

def heapIncreaseKey(heap, key):
    heap[len(heap) - 1] = key
    i = len(heap) - 1
    while i > 0 and heap[parent(i)] < heap[i]:
        heap[i], heap[parent(i)] = heap[parent(i)], heap[i]
        i = parent(i)

def heapExtractMax(heap):
    if len(heap) < 1:
        return None
    else:
        MAX = heap[0]
        heap[0] = heap.pop()
        maxHeapify(heap, 0)

        return MAX

def maxHeapify(heap, i):
    left = (i + 1) * 2 - 1
    right = (i + 1) * 2
    largest = left if left < len(heap) and heap[left] > heap[i] else i
    largest = right if right < len(heap) and heap[right] > heap[largest] else largest
    if largest != i:
        heap[i], heap[largest] = heap[largest], heap[i]
        maxHeapify(heap, largest)


heap = []
while True:
    line = sys.stdin.readline()
    if line[0] == 'i':
        insert(heap, int(line.split()[1]))
    elif line[0:2] == 'ex':
        print(str(heapExtractMax(heap)))
    else:
        break

# Fixed version:
from sys import stdin
from heapq import heappop, heappush
readline = stdin.readline

heap = []
while True:
    line = readline()
    if line[1] == 'x':
        print(-heappop(heap))
    elif line[0] == 'i':
        heappush(heap, -int(line.split()[1]))
    else:
        break


'''
def insert(heap, key):
    heap.append(-1000000)
    heapIncreaseKey(heap, key)

def parent(i):
    return (i - 1) // 2

def heapIncreaseKey(heap, key):
    heap[len(heap) - 1] = key
    i = len(heap) - 1
    while i > 0 and heap[parent(i)] < heap[i]:
        heap[i], heap[parent(i)] = heap[parent(i)], heap[i]
        i = parent(i)

def heapExtractMax(heap):
    if len(heap) < 1:
        return None
    else:
        MAX = heap[0]
        heap[0] = heap.pop()
        maxHeapify(heap, 0)

        return MAX

def maxHeapify(heap, i):
    left = (i + 1) * 2 - 1
    right = (i + 1) * 2
    largest = left if left < len(heap) and heap[left] > heap[i] else i
    largest = right if right < len(heap) and heap[right] > heap[largest] else largest
    if largest != i:
        heap[i], heap[largest] = heap[largest], heap[i]
        maxHeapify(heap, largest)


heap = []
while True:
    line = sys.stdin.readline()
    if line[0] == 'i':
        insert(heap, int(line.split()[1]))
    elif line[0:2] == 'ex':
        print(str(heapExtractMax(heap)))
    else:
        break
'''

# Buggy version:
import sys
from heapq import *
H,O=[],''
for e in sys.stdin:
 if'i'==e[0]:heappush(H,-int(e[7:]))
 elif't'==e[2]:O+=f'{-heappop(H)}\n'
print(O[-1])


# Fixed version:
import sys
from heapq import *
H=[]
O=''
for e in sys.stdin:
 if'i'==e[0]:heappush(H,-int(e[7:]))
 elif'x'==e[1]:O+=f'{-heappop(H)}\n'
print(O[:-1])

# Buggy version:
import sys
from heapq import *
H,O=[],[]
for e in sys.stdin:
 c=e[1]
 if'n'==c:heappush(H,-int(e[7:]))
 elif'x'==c:O+=[-heappop(H)]
print('\n'.join(map(str,O)))


# Fixed version:
import sys
from heapq import *
H,O=[],[]
for e in sys.stdin:
 if'i'==e[0]:heappush(H,-int(e[7:]))
 elif'x'==e[1]:O+=[-heappop(H)]
print('\n'.join(map(str,O)))