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)

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        Heap piorityQueue = new Heap();
        while(true)
        {
            string[] inputs = Console.ReadLine().Split(' ');
            switch (inputs[0])
            {
                case "insert": piorityQueue.Insert(int.Parse(inputs[1])); break;
                case "extract": Console.WriteLine(piorityQueue.ExtractMax()); break;
                case "end": return;
            }
        }
    }
}

class Heap
{
    private const int MaxSize = 2000000;
    public int Count {get; set;} // = 0;
    private int[] elements;
    public int this[int i]
    {
        get {return elements[i];}
        set {elements[i] = value;}
    }
    public Heap() : this(MaxSize) {}
    public Heap(int size)
    {
        elements = new int[size];
        Count = 0;
    }
    public Heap(int[] array)
    {
        Count = array.Length;
        elements = new int[Count];
        for (int i = 0; i < Count; i++)
        {
            elements[i] = array[i];
        }
    }
    private int LeftOf(int i)
    {
        int val = 2 * (i + 1) - 1;
        return val < Count ? val : -1;
    }
    private int RightOf(int i)
    {
        int val = 2 * (i + 1);
        return val < Count ? val : -1;
    }
    private int ParentOf(int i)
    {
        int val = (i + 1) / 2 - 1;
        return val >= 0 ? val : -1;
    }
    public void BuildMaxHeap()
    {
        for (int i = Count / 2 - 1; i >= 0 ; i--)
        {
            Modify(i);
        }
    }
    private void Modify(int i)
    {
        int l = LeftOf(i);
        int r = RightOf(i);
        int largest = i;
        if(l != -1 && elements[l] > elements[i]) largest = l;
        if(r != -1 && elements[r] > elements[largest]) largest = r;
        if(largest != i)
        {
            Swap(i, largest);
            Modify(largest);
        }
    }
    private void Swap(int i, int j)
    {
        int tmp = elements[i];
        elements[i] = elements[j];
        elements[j] = tmp;
    }
    public void Display()
    {
        for (int i = 0; i < Count; i++)
        {
            Console.Write(" " + elements[i]);
        }
        Console.WriteLine();
    }

    public void Insert(int val)
    {
        int i = Count++;
        elements[i] = val;
        while(i > 0 && elements[ParentOf(i)] < elements[i])
        {
            Swap(ParentOf(i), i);
            i = ParentOf(i);
        }
    }
    public int ExtractMax()
    {
        if(Count <= 0) return -1;
        int tmp = elements[0];
        elements[0] = elements[--Count];
        Modify(0);
        return tmp;
    }
}