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.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lec09C
{
    class Program
    {
        static int size;
        public const int INF = 2000000009;

        static void Main(string[] args)
        {
            //setting part
            size = 0;
            int[] array = new int[2000000];
            bool flag = true;


            //main part
            while (flag)
            {
                string input = Console.ReadLine();
                string[] split = input.Split(' ');

                if (split[0] == "insert")
                {
                    MaxHeapInsert(array, int.Parse(split[1]));
                }
                if (split[0] == "extract")
                {
                    int k = HeapExtractMax(array);
                    Console.WriteLine("{0}", k);
                }
                if (split[0] == "end")
                {
                    flag = false;
                }
            }


            Console.ReadLine();
        }

        static void MaxHeapInsert(int [] arr, int key)
        {
            size++;
            arr[size-1] = -1 * INF;
            HeapIncreaseKey(arr, key);
        }

        static void HeapIncreaseKey( int [] arr , int key)
        {
            int i = size;
            if (key < arr[i-1])
            {
                Console.WriteLine("HeapIncreaseKey Error");
            }
            arr[i-1] = key;
            while (i > 1 && arr[(int)(i /2) -1] < arr[i -1])
            {
                int temp = arr[(int)(i / 2) - 1];
                arr[(int)(i / 2) - 1] = arr[i - 1];
                arr[i - 1] = temp;
                i = i/2;
            }
        }

       static int HeapExtractMax(int [] arr)
        {
            if (size < 1)
            {
                Console.WriteLine("HeapExtractMax Error");
            }
            int max = arr[0];
            arr[0] = arr[size - 1];
            size--;
            MaxHeapify(arr,1);
            return max;
        }

        static void BuildMaxHeap(int[] arr)
        {
            for (int i = size / 2; i > 0; i--)
            {
                MaxHeapify(arr, i);
            }
        }

        static void MaxHeapify(int[] arr, int i)
        {
            int l = i * 2;
            int r = i * 2 + 1;
            int largest;

            if (l <= size && arr[l - 1] > arr[i - 1])
            {
                largest = l;
            }
            else
            {
                largest = i;
            }
            if (r <= size && arr[r - 1] > arr[largest - 1])
            {
                largest = r;
            }

            if (largest != i)
            {
                int temp = arr[i - 1];
                arr[i - 1] = arr[largest - 1];
                arr[largest - 1] = temp;

                MaxHeapify(arr, largest);
            }
        }
    }
}