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 M 2000000
#define CENTER(i) ((i)>>1)
#define WHITE(i)  ((i)<<1)
#define BLACK(i)  (((i)<<1)+1)

typedef struct{
  int k;
}WAG;

int cc;
WAG c[M+5];

void extract(int i){
  int w,b,ex;
  WAG rt;
  w = WHITE(i),b = BLACK(i);
  if(w < cc && c[w].k > c[i].k){
    ex = w;
  }
  else{
    ex = i;
  }
  if(b < cc && c[b].k > c[ex].k){
    ex = b;
  }
  if(ex != i){
    rt = c[i],c[i] = c[ex],c[ex] = rt;
    extract(ex);
  }
}

int rise(){
  int g = c[0].k;
  c[0] = c[--cc];
  extract(0);
  return g;
}

void delm(int k){
  int i,ex;
  WAG rt;

  i = cc++;
  c[i].k = k;
  while(i>0&&c[ex=CENTER(i)].k < c[i].k){
    rt = c[i];
    c[i] = c[ex];
    c[ex] = rt;
    i = ex;
  }
}

#if 1
#define gun() getchar_unlocked()
#define pun(c) putchar_unlocked(c)
#else 
#define gun() getchar()
#define pun(c) putchar(c)
#endif

int gp(){
  int m = 0;
  int c = gun();
  do m = 10 * m + (c & 0xf), c = gun(); while(c >= '0');
  return m;
}

char a[20];
void bump(int m){
  int i = 0;
  if(m==0)
    a[i++] = '0';  
  else
    while(m)
      a[i++] = m % 10 + '0',m/=10;
  while(i)
    pun(a[--i]);
    pun('\n');
}

int main(){
  int l;

  cc = 0;

  while(1){
    if(gun()=='e'){
      if(gun()=='n')
	break;
      l = 6;
      while(l--)
	gun();
      bump(rise());
    }
    else{
      l=6;
      while(l--)
	gun();
      delm(gp());   
    }
  }
  return 0;
}