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>
#include <math.h>
#define SWAP(a, b) (a += b, b = a - b, a -= b)

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

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

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

void upHeap(int *H, int i)
{
    while (parent(i) >= 1 && H[i] > H[parent(i)])
    {
        SWAP(H[i], H[parent(i)]);
        i = parent(i);
    }
}

void insert(int *H, int *hsize, int key)
{
    *hsize += 1;
    H[*hsize] = key;
    upHeap(H, *hsize);
}

void maxHeapify(int *H, int *hsize, int i)
{
    int u = i;
    int l = left(u);
    int r = right(u);
    int largest;

    if (l <= *hsize && H[l] > H[u])
    {
        largest = l;
    }
    else
    {
        largest = u;
    }
    if (r <= *hsize && H[r] > H[largest])
    {
        largest = r;
    }

    if (largest != u)
    {
        SWAP(H[u], H[largest]);
        maxHeapify(H, hsize, largest);
    }
}

int extractMax(int *H, int *hsize)
{
    int max = H[1];
    H[1] = H[*hsize];
    *hsize -= 1;

    maxHeapify(H, hsize, 1);

    return max;
}

int main(void)
{
    int H[2000001];
    char command[10];
    int hsize = 0;
    int key;
    int max_value;

    while (1)
    {
        scanf("%s", command);
        if (command[0] == 'e' && command[1] == 'n')
        {
            break;
        }
        else if (command[0] == 'i')
        {
            scanf("%d", &key);
            insert(H, &hsize, key);
        }
        else if (command[0] == 'e')
        {
            max_value = extractMax(H, &hsize);
            printf("%d\n", max_value);
        }
    }

    return 0;
}