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 MAX_HEAP_SIZE 2000000
#define COMMAND_LEN 24
#define INF_POS 2000000001
#define INF_NEG -2000000001

int heap[MAX_HEAP_SIZE + 1];

int hsize = 0;

int left(int id);

int right(int id);

int parent(int id);

void maxHeapify(int rootId);
void buildMaxHeap(void);

int heapMaximum();
int heapExtractMax();
void heapIncreaseKey(int id, int key);
void maxHeapInsert(int key);

int main(){
  int n;
  char strBuf[COMMAND_LEN];

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

    if(strcmp("end", strBuf) == 0){
      break;
    }
    else if(strcmp("insert", strBuf) == 0){
      scanf("%d", &n);
      maxHeapInsert(n);
    }
    else if(strcmp("extract", strBuf) == 0){
      printf("%d\n", heapExtractMax());
    }
    else {
      printf("ERROR: INVALID COMMAND\n");
      exit(1);
    }
  }
  
  return 0;
}

int parent(int id){
  return id/2;
}

int left(int id){
  return 2*id;
}

int right(int id){
  return 2*id + 1;
}

void maxHeapify(int rootId){
  int l = left(rootId);
  int r = right(rootId);
  int largest;
  
  //左の子、自分、右の子で値が最大のノードを選ぶ
  if(l <= hsize && heap[l] > heap[rootId]){
    largest = l;
  }
  else {
    largest = rootId;
  }

  if(r <= hsize && heap[r] > heap[largest]){
    largest = r;
  }

  int tmp;
  if(largest != rootId){ //rootIdの子の方が値が大きいとき
    tmp = heap[rootId];
    heap[rootId] = heap[largest];
    heap[largest] = tmp;
    maxHeapify(largest);
  }
}

void buildMaxHeap(){
  for(int i = hsize/2 ; i >= 1; i--){
    maxHeapify(i);
  }
}
    
int heapMaximum(){
  return heap[1];
}

int heapExtractMax(){
  if(hsize < 1){
    printf("ERROR: HEAP UNDER FLOW\n");
    exit(1);
  }

  int max = heap[1];
  heap[1] = heap[hsize];
  hsize--;
  maxHeapify(1);
  return max;
}
    
void heapIncreaseKey(int id, int key){
  if(key < heap[id]){
    printf("ERROR: KEY VALUE IS SMALLER");
    exit(1);
  }
  heap[id] = key;
  while(id > 1 && heap[parent(id)] < heap[id]){
    int tmp = heap[id];
    heap[id] = heap[parent(id)];
    heap[parent(id)] = tmp;
    id = parent(id);
  }
}

void maxHeapInsert(int key){
  hsize++;
  heap[hsize] = INF_NEG;
  heapIncreaseKey(hsize, key);
}