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: Wrong Answer)

# max???????????即?????????????????????????????????
# node[i]??????node???max???????????即??則????????????????????????
def maxHeapify(heap_tree, i)
  heap_size = heap_tree.size
  #i_p = (i - 1) / 2
  i_l = 2 * i + 1
  i_r = 2 * i + 2
  largest = i

  if i_l <= heap_size - 1
    case heap_tree[i_l] <=> heap_tree[i]
    when 1
      largest = i_l
    end
  end

  if i_r <= heap_size - 1
    case heap_tree[i_r] <=> heap_tree[largest]
    when 1
      largest = i_r
    end
  end

  if largest != i
    heap_tree[i], heap_tree[largest] = heap_tree[largest], heap_tree[i]
    maxHeapify(heap_tree, largest)
  end
end

# max???????????即???????卒????????????????
def maxHeapInsert(heap, x)
  heap.push(x)
  i = heap.size - 1
  i_p = (i - 1) / 2
  while i > 0 and heap[i_p] < heap[i]
    heap[i_p], heap[i] = heap[i], heap[i_p]
    i_p = (i - 1) / 2
    i = i_p
  end
end

# max???????????即????????????????????????
def maxHeapExtract(heap)
  result = heap.shift
  heap.unshift(heap.pop)
  maxHeapify(heap, 0)
  return result
end

def buildMaxHeap(heap_tree)
  ((heap_tree.size - 1) / 2).downto(0) do |i|
    maxHeapify(heap_tree, i)
  end
end

heap = []
while line = gets
  cmd = line.chomp.split(" ")
  case cmd[0]  
  when "insert"
    maxHeapInsert(heap, cmd[1].to_i)
  when "extract"
    puts maxHeapExtract(heap)
  when "end"
    break
  end
end