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 <unistd.h>
#include <string.h>

#define SIZE 2000000

int main() {
  static int A[SIZE];
  int H=0;
  int i, l, largest, tmp, n;
  static char com[21], buf[20];
  buf[19]='\n';
  char outbuf[3000];
  int outlen=0;
  char *p;
  do {
    fgets(com, 20, stdin);
    if (*com == 'i') { // insert
      A[++H] = 0;
      p = com+7;
      do {
        A[H] = A[H]*10 + *p-'0';
        p++;
      } while (*p&0x20);  // (*p!='\n');
      for (i=H; i>1 && A[i>>1]<A[i];i>>=1) {
        tmp = A[i>>1];
        A[i>>1] = A[i];
        A[i] = tmp;
      }
    } else if (com[1]=='x') { // extract
      n = A[1];
      for (i=18; n>0; n/=10) buf[i--]=(n%10)+'0';
      //      fputs(buf+i+1, stdout);
      strncpy(outbuf+outlen, buf+i+1, 19-i);
      outlen += 19-i;
      if (outlen>2980) {
        write(1, outbuf, outlen);
        outlen=0;
      }
      
      A[1] = A[H];
      i = 1;
      while (1) {
        l = i<<1;
        largest = i;
        if (l < H && A[l] > A[i])        largest = l;
        if (l+1 < H && A[l+1] > A[largest])  largest = l+1;
        if (largest == i) break;
        tmp = A[i];
        A[i] = A[largest];
        A[largest] = tmp;
        i = largest;
      }
      H--;
    } else {
      write(1, outbuf, outlen);
      break;
    }
  } while (1); //(com[2]!='d'); // end
  return 0;
}