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;

namespace CSharpSample01
{
    
    class PriorityQueue
    {
        int H;
        int[] heap;
        const int MAX = 2000000;

        public PriorityQueue()
        {
            H = 0;
            heap = new int[MAX];
        }
        
        public PriorityQueue(int size)
        {
            H = size;
            heap = new int[H + 1];
        }

        public void MaxHeapfy(int p)
        {
            while(2 * p <= H)
            {
                int son_n = 2 * p;
                int son = heap[2 * p];
                if(2 * p + 1 <= H && son < heap[2 * p + 1])
                {
                    son = heap[2 * p + 1];
                    son_n++;
                }

                if(son > heap[p])
                {
                    int tmp = heap[p];
                    heap[p] = son;
                    heap[son_n] = tmp;
                }
                else
                {
                    break;
                }

                p = son_n;
            }
        }

        public void BuildMaxHeap()
        {
            for(int s = H / 2; s >= 1; s--)
            {
                MaxHeapfy(s);
            }
        }

        public void Insert(int key)
        {
            H++;
            heap[H] = key;

            int son = H;

            while(son != 1)
            {
                int p = son / 2;
                if(heap[son] > heap[p])
                {
                    int tmp = heap[p];
                    heap[p] = heap[son];
                    heap[son] = tmp;
                    son /= 2;
                }
                else
                {
                    break;
                }
            }

        }

        public int ExtractMax()
        {
            int max = heap[1];
            heap[1] = heap[H];
            H--;

            MaxHeapfy(1);

            return max;
        }

    }


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

            while (true)
            {
                string[] input = Console.ReadLine().Split(' ');

                string order = input[0];
                int key;
                
                if(order == "insert")
                {
                    key = int.Parse(input[1]);
                    pq.Insert(key);
                }
                else if(order == "extract")
                {
                    Console.WriteLine(pq.ExtractMax());
                }
                else if(order == "end")
                {
                    break;
                }
                
            }
            
        }
    }
}