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 <algorithm>
#include <iostream>

using namespace std;

class Heap{
public:
	Heap(){
		array = new long long[2000000];
		n = 0;
	}
	void insert(long long data){
		array[n] = data;
		int tmp = n;
		while(tmp != 0 && array[tmp] > array[(tmp-1)/2]){
			std::swap(array[tmp], array[(tmp-1)/2]);
			tmp = (tmp-1)/2;
		}
		n++;
	}
	long long extract(){
		int ret = array[0];
		n--;
		std::swap(array[0],array[n]);
		int tmp = 0;
		while(2*tmp+1 <= n-1){
			if(2*tmp+2 <= n-1){
				if(array[2*tmp+2] > array[tmp] && array[2*tmp+2] >= array[2*tmp+1]){
					std::swap(array[2*tmp+2],array[tmp]);
					tmp = 2*tmp+2;
				}else if(array[2*tmp+1] > array[tmp] && array[2*tmp+1] >= array[2*tmp+2]){
					std::swap(array[2*tmp+1],array[tmp]);
					tmp = 2*tmp+1;
				}else{
					break;
				}
			}else{
				if(array[tmp] < array[2*tmp+1]){
					std::swap(array[tmp],array[2*tmp+1]);
					tmp = 2*tmp+1;
				}else{
					break;
				}
			}
		}
		return ret;
	}

private:
	long long* array;
	int n;
};

int main(){

	long long insert_data;
	char buf[22];
	Heap heap;

	while(true){
		fgets(buf,22,stdin);
		if(buf[2] == 'd') break;
		if(buf[0] == 'i'){
			insert_data = 0;
			for(int i = 0; buf[i] != '\0';i++){
				if(buf[i] >= '0' && buf[i] <= '9'){
					insert_data = 10*insert_data + (buf[i] - '0');
				}
			}
			heap.insert(insert_data);
		}else{
			printf("%lld\n",heap.extract());
		}
	}
}