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: Wrong Answer)

#include<stdio.h>
#include<stdlib.h>
#define MAX (2000000 + 1)
typedef struct node{
  int key;
  struct node *p, *l, *r;
}T_node;
T_node **heap;
int n;
void mahpy(int);
void insert(int);
int extract();
int main(){
  char buf[256];
  int v;
  n = 0;
  heap = (T_node **)malloc(sizeof(T_node *) * MAX);
  int i;
  for(i = MAX; i > MAX / 2; i--){
    heap[i] = (T_node *)malloc(sizeof(T_node));
    heap[i]->key = -1;
    heap[i]->p = heap[i]->l = heap[i]->r = NULL;
  }
  for(i = MAX / 2; i > 0; i--){
    heap[i] = (T_node *)malloc(sizeof(T_node));
    heap[i]->key = -1;
    heap[i]->l = heap[i * 2];
    heap[i]->r = heap[i * 2 + 1];
    heap[i * 2]->p = heap[i * 2 + 1]->p = heap[i];
  }
  heap[1]->p = NULL;
  while(scanf("%s", buf) & strcmp(buf, "end")){
    if(!strcmp(buf, "insert")){
      n++;
      scanf("%d", &v);
      insert(v);
    }
    else if(!strcmp(buf, "extract")){
      printf("%d\n", extract());
      n--;
    }
  }
  for(i = MAX; i > 0; i--)
    free(heap[i]);
  free(heap);
  return 0;
}

void insert(int v){
  heap[n]->key = v;
/*  if(n /2){
    heap[n]->p = heap[n / 2];
    if(n % 2)
      heap[n / 2]->r = heap[n];
    else
      heap[n / 2]->l = heap[n];
  }*/
  T_node *ma;
  for(ma = heap[n]; ma->p != NULL; ma = ma->p){
    if(ma->key > ma->p->key){
      ma->key = ma->p->key;
      ma->p->key = v;
    }
    else
      break;
  }
/*  int i;
  for(i = n / 2; i > 0; i--)
    mahpy(i);*/
}

int extract(){
  int tmp = heap[1]->key;
  heap[1]->key = -1;
  int i;
  //  for(i = n / 2; i > 0; i--)
    mahpy(1);
  return tmp;
}

void mahpy(int i){
  T_node *max;
  int tmp, ni = 0;
  if(heap[i]->r != NULL){
    if(heap[i]->l->key >= heap[i]->r->key){
      max = heap[i]->l;
            if(max->key >= 0)
	ni = i * 2;
    }
    else{
      max = heap[i]->r;
            if(max->key >= 0)
	ni = i * 2 + 1;
    }
  }
  else if(heap[i]->l != NULL){
    max = heap[i]->l;
        if(max->key >= 0)
      ni = i * 2;
  }
  else
    max = heap[i];
  if(max->key > heap[i]->key){
    tmp = heap[i]->key;
    heap[i]->key = max->key;
    max->key = tmp;
  }
  if(ni)
    mahpy(ni);
  else if(heap[i]->key < 0){
    heap[i]->key = heap[n]->key;
    heap[n]->key = -1;
/*    heap[n]->p = heap[n]->l = heap[i]->r = NULL;
    if(n % 2)
      heap[n / 2]->r = NULL;
    else
      heap[n / 2]->l = NULL;*/
  }
}