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 C (judge verdict: Accepted)

#include<stdio.h>
#define MAX 2000010
#define OVERMIN -1

int heap[MAX];
int H = 0;

void swap(int i, int j) {
	int buf = heap[i];
	heap[i] = heap[j];
	heap[j] = buf;
}

void maxHeap(int H, int i) {
	int l = 2 * i, r = 2 * i + 1;
	int largest = i;
	if (l <= H && heap[l] > heap[i])largest = l;
	if (r <= H && heap[r] > heap[largest])largest = r;
	if (largest != i) {
		swap(i, largest);
		maxHeap(H, largest);
	}
}

void InsertKey(int H, int k) {
	int pos = H;
	if (heap[H] > k) {
		printf("Insertion Error\n");
		return;
	}
	else {
		heap[H] = k;
		while (pos / 2 > 0 && heap[pos] > heap[pos / 2]) {
			swap(pos, pos / 2);
			pos /= 2;
		}
	}
}

void Insert(int k) {
	H++;
	heap[H] = OVERMIN;
	InsertKey(H, k);
}

int extractMax() {
	int out = heap[1];
	heap[1] = heap[H];
	H--;
	maxHeap(H, 1);
	return out;
}

int main() {
	char input_str[10];
	int input_ele;

	while (1) {
		scanf("%s", input_str);
		if (input_str[0] == 'i') {
			scanf("%d", &input_ele);
			Insert(input_ele);
		}
		else {
			if (input_str[1] == 'x') {
				printf("%d\n", extractMax());
			}
			else break;
		}
	}

	return 0;
}