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 Haskell (judge verdict: Time Limit Exceeded)

{-# LANGUAGE OverloadedStrings #-}

import           Control.Applicative
import           Control.Monad
import qualified Data.ByteString.Char8 as B

import           Data.Array.IArray
import           Data.Array.IO
import           Data.Array.MArray

readi :: B.ByteString -> Int
readi b | Just (n, _) <- B.readInt b = n

data MaxHeap = MaxHeap !Int !(IOUArray Int Int)

emptyHeap :: Int -> IO MaxHeap
emptyHeap size = do
    arr <- newArray (0, size) minBound
    return $ MaxHeap 0 arr

buildMaxHeap :: Int -> MaxHeap -> IO ()
buildMaxHeap n h = do
    let e = n `div` 2
    forM_ [e, e-1 .. 1] $ \i -> do
        maxHeapify i h

maxHeapify :: Int -> MaxHeap -> IO ()
maxHeapify i h@(MaxHeap n arr) = do
    vi <- readArray arr i
    max1 <-
        if i * 2 > n
            then return i
            else do
                t1 <- readArray arr (i * 2)
                if t1 > vi
                    then return (i * 2)
                    else return i
    max2 <-
        if i * 2 + 1 > n
            then return max1
            else do
                t2 <- readArray arr (i * 2 + 1)
                t1 <- readArray arr max1
                if t2 > t1
                    then return (i * 2 + 1)
                    else return max1
    when (max2 /= i) $ do
        swap i max2 arr
        maxHeapify max2 h

swap :: Int -> Int -> IOUArray Int Int -> IO ()
swap i j arr = do
    vi <- readArray arr i
    vj <- readArray arr j
    writeArray arr i vj
    writeArray arr j vi

insert :: Int -> MaxHeap -> IO MaxHeap
insert k (MaxHeap n arr) = do
    writeArray arr (n + 1) k
    bubbleUp (n + 1) arr
    return $ MaxHeap (n + 1) arr

bubbleUp :: Int -> IOUArray Int Int -> IO ()
bubbleUp i arr = do
    let p = i `div` 2
    when (p >= 1) $ do
        vi <- readArray arr i
        vp <- readArray arr p
        when (vi > vp) $ do
            swap i p arr
            bubbleUp p arr

extractMax :: MaxHeap -> IO (Int, MaxHeap)
extractMax (MaxHeap n arr) = do
    when (n <= 0) $ error "extract from empty heap"
    r <- readArray arr 1
    vn <- readArray arr n
    writeArray arr 1 vn
    let h = MaxHeap (n - 1) arr
    buildMaxHeap (n - 1) h
    return (r, h)

solve :: MaxHeap -> [[B.ByteString]] -> IO ()
solve _ [] = return ()
solve _ (["end"]:_) = return ()
solve h ([_]:xss) = do
    (n, h') <- extractMax h
    print n
    solve h' xss
solve h ([_, bn]:xss) = do
    let k = readi bn
    h' <- insert k h
    solve h' xss

main :: IO ()
main = do
    xss <- fmap B.words . B.lines <$> B.getContents
    heap <- emptyHeap 2000001
    solve heap xss