Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Priority Queue

A priority queue is a data structure which maintains a set $S$ of elements, each of with an associated value (key), and supports the following operations:

$insert(S, k)$: insert an element $k$ into the set $S$

$extractMax(S)$: remove and return the element of $S$ with the largest key

Write a program which performs the $insert(S, k)$ and $extractMax(S)$ operations to a priority queue $S$.
The priority queue manages a set of integers, which are also keys for the priority.

Input

Multiple operations to the priority queue $S$ are given. Each operation is given by "insert $k$", "extract" or "end" in a line. Here, $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 $\leq 2,000,000$

$0 \leq k \leq 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

Solution in Python3 (judge verdict: Time Limit Exceeded)

def get_parent(A, i):
    if i == 1:
        return None
    else:
        return i // 2

def get_left(A, i):
    if len(A) > 2 * i:
        return 2 * i
    else:
        return None

def get_right(A, i):
    if len(A) > 2 * i + 1:
        return 2 * i + 1
    else:
        return None

A = [-1] * 2000000
H = 0

def insert(A, key):
    global H
    H += 1
    A[H] = key
    i = H
    while i > 1:
        # print(i)
        parent = get_parent(A, i)
        if parent is None:
            break
        if A[parent] < A[i]:
            # print('swap', i, parent, A[i], A[parent])
            tmp = A[i]
            A[i] = A[parent]
            A[parent] = tmp
            i = parent
        else:
            break

def max_heapfy(A, i):
    left = get_left(A, i)
    right = get_right(A, i)
    largest = i
    if left and A[left] > A[i]:
        largest = left
    if right and A[right] > A[largest]:
        largest = right
    if largest != i:
        tmp = A[i]
        A[i] = A[largest]
        A[largest] = tmp
        max_heapfy(A, largest)

def extract_max(A):
    global H
    max_value = A[1]
    A[1] = A[H]
    A[H] = -1
    H -= 1
    max_heapfy(A, 1)
    return max_value

# Heap heap ヒープの実装はここを参考にした
# http://ikapblg.blog.fc2.com/blog-entry-57.html

def print_heap(A):
    for index, elem in enumerate(A):
        if index == 0:
            continue
        if elem == -1:
            break
        print(elem)

while True:
    line = input().split()
    if line[0] == 'insert':
        insert(A, int(line[1]))
        # print(A)
    elif line[0] == 'extract':
        max_value = extract_max(A)
        print(max_value)

    else:
        # print('end:')
        # print_heap(A)
        break