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 <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX 2000001
/*
typedef struct{
  int id;
  int parent;
  int left;
  int right;
}Tree;
*/
int *A;
int heap_size = 0;

void maxHeapify(int i){
  int l, r;
  int largest;
  int tmp;

  l=i*2;
  r=i*2+1;

  if((l <= heap_size) && (A[l] > A[i])){
    largest = l;
  }else{
    largest = i;
  }
  if ((r <= heap_size) && (A[r] > A[largest])) {
    largest = r;
  }
  //printf("largest:%d\n",largest );
  //printf("A[largest]:%d\n",A[largest]);
  if(largest != i){
    //printf("A[%d]:%d\n",i,A[i] );
    tmp = A[i];
    A[i] = A[largest];
    A[largest] = tmp;
    maxHeapify(largest);
  }
}

void heapIncreaseKey(int i, int key){
  int tmp;
  int parent = i/2;
  if(key < A[i]){
    return;
  }
  A[i] = key;
  while (i > 1 && A[parent] < A[i]) {
    //printf("in\n");
    tmp = A[i];
    A[i] = A[parent];
    A[parent] = tmp;
    i = parent;
  }
  //printf("A[i]:%d\n",A[i]);
}

void insert(int n){

  heap_size++;
  A[heap_size] = INT_MIN;
  heapIncreaseKey(heap_size, n);
}

void buildHeap(){
  int i;
  for(i = heap_size/2; i >= 1; i--){
    if(i==0) i=1;
    maxHeapify(i);
  }
}

void heapExtructMax(){
  A[1] = A[heap_size];
  heap_size--;
  //maxHeapify(1);
  buildHeap();
}

void printer(int p){
  int i;
  for (i = 1; i <= p; i++) {
    printf(" %d",A[i]);
  }
  printf("\n");
}

int main() {
  int n;
  char com[10];

  A = (int *)malloc(sizeof(int) * MAX);

  while (1) {
    //printer(heap_size);
    scanf("%s",com);
    if(0==strcmp(com,"end")){
      break;
    }else if(com[0]=='i'){
      //printf("increase\n");
      scanf("%d",&n);
      insert(n);
      //printer(heap_size);
    }else if(0==strcmp(com,"extract")){
      printf("%d\n",A[1]);
      heapExtructMax();
    }else{
      printf("test\n");
      break;
    }
    buildHeap();
    //printf("heap_size:%d\n",heap_size);
  }
  //printf("\n");

  //buildHeap();

  free(A);
  return 0;
}