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>
#include<stdlib.h>
#include<string.h>

#define SIZE 2000000

int heap[SIZE];
int n;

void up(int i){
	if( i/2 <= 0 ){ return; }
	if( heap[i/2] > heap[i] ){ return; }
	int temp = heap[i/2];
	heap[i/2] = heap[i];
	heap[i] = temp;
	up(i/2);
	return;
}

void add(int key){
	heap[++n] = key;
	up(n);
	return;
}

void down(int i){
	if( 2*i > n ){
		return;
	}else if( 2*i+1 > n ){
		if( heap[2*i] > heap[i] ){
			int temp = heap[2*i];
			heap[2*i] = heap[i];
			heap[i] = temp;
		}
	}else{
		int j = (heap[i]>heap[2*i]?i:2*i);
		j = (heap[j]>heap[2*i+1]?j:2*i+1);
		if( j == i ){ return; }
		int temp = heap[j];
		heap[j] = heap[i];
		heap[i] = temp;
		down(j);
	}
	return;
}

int extract(){
	int re = heap[1];
	heap[1] = heap[n--];
	down(1);
	return re;
}

int main(){
	int i;
	char com[256];
	while( 1 ){
		scanf("%s", com);
		if( strcmp(com, "end") == 0 ){
			break;
		}else if( strcmp(com, "insert") == 0 ){
			scanf("%d", &i);
			add(i);
		}else if( strcmp(com, "extract") == 0 ){
			printf("%d\n", extract());
		}
	}
	return 0;
}