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.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace aizu1
{
    public class Solution
    {
        public static void Main(string[] args)
        {
            string com;
            int[] A = new int[2000000];
            int H=0;
            StringBuilder builder=new StringBuilder();
            while ((com = Console.ReadLine())!="end")
            {
                var coms = com.Split(' ');
                switch (coms[0])
                {
                    case "insert": Insert(A, int.Parse(coms[1]),ref H);
                        break;
                    case "extract": Extract(A, ref H, builder);
                        break;
                    default: break;
                }
            }
            Console.Write(builder);
        }

        public static void Insert(int[] A, int num, ref int H)
        {
            A[H++]=num;
            HeapUp(A,H-1);
        }

        public static void Extract(int[] A, ref int H, StringBuilder builder)
        {
            if (H > 0)
            {
                builder.Append(A[0]).Append("\n");
                int tmp = A[0];
                A[0] = A[--H];
                A[H] = tmp;
                Maxify(A, 0, H);
            }
        }

        public static void Maxify(int[] A, int i, int H)
        {
            int largest;
            int tmp;
            int left = 2 * i + 1;
            int right = 2 * i + 2;
            if (left < H && A[left] > A[i]) largest = left;
            else largest = i;
            if (right < H && A[right] > A[largest]) largest = right;

            if (largest != i)
            {
                tmp = A[largest];
                A[largest] = A[i];
                A[i] = tmp;
                Maxify(A,largest,H);
            } 
        }
        public static void HeapUp(int[] A, int H)
        {
            int parent = (H-1) / 2;
            int tmp;
            while (parent >= 0)
            {
                if (A[H] > A[parent])
                {
                    tmp = A[H];
                    A[H] = A[parent];
                    A[parent] = tmp;
                    H = parent;
                    parent = (parent - 1) / 2;
                }
                else break;
            }
        }
    }
}