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

$time_i = 0
$time_e = 0
class Maximum
  def initialize(arr = [])
    @arr = arr
    monky
  end
  def monky
    def @arr.[](i)
      super(i - 1)
    end
    def @arr.[]=(i, value)
      super(i - 1, value)
    end
  end
  def buildHeap(root = 1)
    left, right = root * 2, root * 2 + 1
    if @arr[right]
      buildHeap(left); buildHeap(right)
      max = [[@arr[left], left], [@arr[right], right]].max
      if max[0] > @arr[root]
        @arr[root], @arr[max[1]] = @arr[max[1]], @arr[root]
        rebuildHeap(max[1])
      end
    elsif @arr[left]
      buildHeap(left)
      if @arr[left] > @arr[root]
        @arr[root], @arr[left] = @arr[left], @arr[root]
        rebuildHeap(left)
      end                                                       
    end
  end
  def rebuildHeap(root = 1)
    left, right = root * 2, root * 2 + 1
    if @arr[right]
      max = [[@arr[left], left], [@arr[right], right]].max
      if max[0] > @arr[root]
        @arr[root], @arr[max[1]] = @arr[max[1]], @arr[root]
        rebuildHeap(max[1])
      end
    elsif @arr[left]
      if @arr[left] > @arr[root]
        @arr[root], @arr[left] = @arr[left], @arr[root]
        rebuildHeap(left)
      end
    end      
  end
  def insert(key)
    #start = Time.now
    @arr.push(key.to_i)
    i = @arr.size
    while i / 2 >= 1 and @arr[i / 2] < key.to_i
      @arr[i], @arr[i / 2] = @arr[i / 2], @arr[i]
      i /= 2
    end
    #$time_i += Time.now - start
  end
  def extract
    #start = Time.now
    puts @arr[1]
    @arr[1] = @arr.pop
    rebuildHeap
    #$time_e = Time.now - start
  end
  def to_s
    @arr.join(" ")
  end
end
def main(input = STDIN)
  heap = Maximum.new
  while (order = input.gets.chomp) != "end"
    heap.send(*order.split(" "))
  end
  #p [$time_i, $time_e]
end
#main(f = File.open('input2.txt'))
#f.close
main