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: Accepted)

class HeapTree
	def initialize
		@node = []
		@n = 0
	end

	private

	def from_root_swap(key)
		right = right_key(key)
		left = left_key(key)
		if  key < 0 
			return nil
		end
		if !@node[left] && !@node[right]
			return nil
		elsif @node[left] && !@node[right]
			if @node[left] > @node[key]
				@node[key], @node[left] =  @node[left], @node[key]
				from_root_swap(left)
			end
		elsif @node[right] && !@node[left]
			if @node[right] > @node[key]
				@node[key], @node[right] =  @node[right], @node[key]
				from_root_swap(right)
			end
		elsif @node[key] <= @node[left] && @node[left] >= @node[right]
			@node[key], @node[left] =  @node[left], @node[key]
			from_root_swap(left)
		elsif @node[key] <= @node[right] && @node[right] >= @node[left]
			@node[key], @node[right] =  @node[right], @node[key]
			from_root_swap(right)
		end
	end

	def from_leaf_swap(key)
		if key == 0
			return nil
		end
		parent_key = (key-1)/2
		if @node[parent_key] < @node[key]
			@node[key], @node[parent_key] = @node[parent_key], @node[key]
			from_leaf_swap(parent_key)
		end
	end

	def extract!
		num = @node[0]
		@node[0] = @node.pop
		@n -= 1
		from_root_swap(0)
		return num
	end

	def insert!(num)
		@node << num
		@n += 1
		from_leaf_swap(@n-1)
	end

	def left(key)
		@node[2*key+1]
	end

	def right(key)
		@node[2*key+2]
	end

	def left_key(key)
		2*key+1
	end

	def right_key(key)
		2*key+2
	end

	# ???????????¢?´¢
	def precedingOrderSearch(key, &func)
		if @node[key]
			func.call(key, @node[key])
			precedingOrderSearch(left(key), &func)
			precedingOrderSearch(right(key), &func)
		end
	end
	# ???????????¢?´¢
	def afterLineOrderSearch(key, &func)
		if @node[key]
			afterLineOrderSearch(left(key), &func)
			afterLineOrderSearch(right(key), &func)
			func.call(key, @node[key])
		end
	end
	#???????????¢?´¢
	def centerOrderSearch(node, &func)
		if @node[key]
			centerOrderSearch(left(key), &func)
			func.call(key, @node[key])
			centerOrderSearch(right(key), &func)
		end
	end

	public

	def insert(num)
		insert!(num)
	end

	def preceding_crawl(&func) #???hoge.precedingCrawl do |key, value| ??????????????????
		precedingOrderSearch(0, &func)
	end

 # ????????????
 def after_line_crawl(&func)
 	afterLineOrderSearch(0, &func)
 end

# ????????????
def center_crawl(&func)
	centerOrderSearch(0, &func)
end

def extract
	extract!
end

def priority_create
	i = 0
	loop{
		key = (@n-1)/2-i
		break if key < 0
		from_root_swap(key)
		i += 1
	}
end

def list
	return @node
end
end


ans = []
head = HeapTree.new
loop {
	str = gets.to_s.chomp!
	if str == "extract"
		ans << head.extract
	elsif str == "end"
		break
	else
		str = str.split
		head.insert str[1].to_i
	end
}


ans.each do |v|
	puts v
end