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 N 2000000
#define INF 100000000

//operation of pushing or popping
void maxHeapInsert( int A[], int key );
void heapIncreaseKey( int A[], int, int key );
int  heapExtractMax( int A[] );

//make a max heap 
void swap( int*, int* );
void maxHeapify( int A[], int i );
void buildMaxHeap( int A[] );

//declare sevaral global variables
int n = 0;

int main(){
    int A[N+1], i, j, data;
    char com[10];

    //input and some procedures
    while( 1 ){
	scanf("%s", com);
	if( com[0] == 'i' ){
	    scanf("%d", &data);
	    maxHeapInsert(A, data);
	}
	else if( com[0] == 'e' && com[1] == 'x' ){
	    data = heapExtractMax(A);
	    printf("%d\n", data);
	}
	else break;//when end command was inputted
    }
    return 0;
}

//----------------------------------------------------------------
inline void swap( int* a, int *b ){
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
    return;
}
void maxHeapify( int A[], int p ){
    int l, r, largest;

    //initialize
    l = 2*p;
    r = 2*p + 1;
    //compare those values of p , l and r
    largest = p;
    if( A[l] > A[p] && l <= n ) largest = l;
    if( A[r] > A[largest] && r <= n ) largest = r;
    //swap those values
    if( largest != p ){
	swap( &A[largest], &A[p] );
	maxHeapify( A, largest );
    }
    return ;
}
void buildMaxHeap( int A[] ){
    int l, p, r, i, heap_size = N;

    for( i=N/2; i>=1; i-- ) maxHeapify( A, i );
    
    return ;
}

//------------------------------------------------------------------
void maxHeapInsert( int A[], int key ){

    n += 1;
    A[n] = -INF;
    heapIncreaseKey( A, n, key );

    return ;
}

void heapIncreaseKey( int A[], int i, int key ){
    int p = i/2;

    A[i] = key;
    while( i>1 && A[i] > A[p] ){
	swap( &A[i], &A[p] );
	i = p;
	p = p/2;
    }
    return ;
}

//------------------------------------------------------------------
int heapExtractMax( int A[] ){
    int max, heap_size = n;

    max = A[1];
    A[1] = A[heap_size];
    n--;
    maxHeapify( A, 1 );

    return max;
}