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: Time Limit Exceeded)

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

void heapify(int q[],int qCnt,int i){
    int l,r,max;
    l=i*2;
    r=i*2+1;
    max=i;
    
    if(l<=qCnt&&q[l-1]>q[max-1])max=l;
    if(r<=qCnt&&q[r-1]>q[max-1])max=r;
    
    int tmp;
    if(max!=i){
        tmp=q[max-1];
        q[max-1]=q[i-1];
        q[i-1]=tmp;
        
        heapify(q,qCnt,max);
    }
}
int main(){
    int i;
    char com[7+1];
    int x;
    int q[2000000];
    int qCnt=0;
    
    while(1){
        scanf("%s \n",com);
        if(strcmp(com,"insert")==0){
            scanf("%d\n",&x);
            q[qCnt++]=x;
            for(i=qCnt/2;i>=0;i--)heapify(q,qCnt,i+1);
        }else if(strcmp(com,"extract")==0){
            printf("%d\n",q[0]);
            q[0]=q[qCnt-1];
            heapify(q,qCnt,1);
            qCnt--;
        }else if(strcmp(com,"end")==0)break;
    }
    
    return 0;
}