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 N 2000000
#define Gard -1

void InsertHeap(int );
void IncreHeap(int,int);
int ExtractHeap();
void MaxHeap(int,int);

int element,Prio[N];
int heapnum;

int main(){
  int count=0;
  int result=0;
  char order[15];
  char *End="end";
  char *Ext="extract";

  while(1){
    scanf("%s",order);

    count++;
    
    if(count==N){
      break;
    }
    
    else if(strcmp(order,End)==0){
      break;
    }


    if(order[0]=='i'){
      scanf("%d",&heapnum);
      InsertHeap(heapnum);
    }

    else if(strcmp(order,Ext)==0){
      result=ExtractHeap();

      if(result==Gard){
	continue;
      }

      else{
	printf("%d\n",result);
      }
      
    }
  }
  
  return 0;
}

void InsertHeap(int heapnum){
  
  element++;

  Prio[element]=Gard;

  IncreHeap(heapnum,element);
 
}

void IncreHeap(int heapnum ,int element){
  int temp;
  
  if(heapnum<Prio[element]){
    return;
  }

  else{
    Prio[element]=heapnum;

    while((element>1)  && (Prio[element/2]<Prio[element])){

      temp=Prio[element];
      Prio[element]=Prio[element/2];
      Prio[element/2]=temp;

      element=element/2;
      
    }
  }
  
}

int ExtractHeap(){
  int MaxH=0;

  if(element<1){
    return Gard;
  }

  else{
    MaxH=Prio[1];
    Prio[1]=Prio[element];
    element--;
    MaxHeap(1,element);
    return MaxH;
  }
}

void MaxHeap(int judge,int element){
  int left,right,temp;
  int Hscale;

  left=2*judge;
  right=2*judge+1;

  if((element>=left) && (Prio[judge]<Prio[left])){
    Hscale=left;
  }

  else{
    Hscale=judge;
  }

  if((element>=right) && (Prio[Hscale]<Prio[right])){
    Hscale=right;
  }

  if(judge!=Hscale){
    temp=Prio[judge];
    Prio[judge]=Prio[Hscale];
    Prio[Hscale]=temp;
    MaxHeap(Hscale,element);
  }
}