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.Generic;
using System.IO;

namespace Heaps
{
    class Priority_Queue
    {
        class PriorityQueue
        {
            List<int> buffer;

            public PriorityQueue() { buffer = new List<int>(); }

            static void PushHeap(List<int> array, int elem)
            {
                var n = array.Count;
                array.Add(elem);

                while (n != 0)
                {
                    var i = (n - 1) / 2;
                    if (array[n] > array[i])
                    {
                        var tmp = array[n];
                        array[n] = array[i];
                        array[i] = tmp;
                    }
                    n = i;
                }
            }

            static void PopHeap(List<int> array)
            {
                var n = array.Count - 1;
                array[0] = array[n];
                array.RemoveAt(array.Count - 1);

                for (int i = 0, j; (j = 2 * i + 1) < n;)
                {
                    if ((j != n - 1) && (array[j].CompareTo(array[j + 1]) < 0))
                        j++;
                    if (array[i] < array[j])
                    {
                        var tmp = array[j];
                        array[j] = array[i];
                        array[i] = tmp;
                    }
                    i = j;
                }
            }

            public void Push(int elem)
            {
                PushHeap(buffer, elem);
            }

            public int Pop()
            {
                var top = buffer[0];
                PopHeap(buffer);
                return top;
            }

        }

        static void Main(string[] args)
        {
            var pq = new PriorityQueue();

            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);

            string line;
            while ((line = Console.ReadLine()) != "end")
                switch (line[0])
                {
                    case 'i':
                        pq.Push(int.Parse(line.Substring(7)));
                        break;
                    case 'e':
                        Console.WriteLine(pq.Pop());
                        break;
                }
            Console.Out.Flush();
        }
    }
}