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 JAVA (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	
	InputUtil in = new InputUtil();

	public static void main(String[] args) throws IOException {
		new Main().exec();
	}

	public void exec() throws IOException {
		PrintWriter out = new PrintWriter(System.out);
		PriorityQueue pq = new PriorityQueue(2000000);
		CommandInfo cmd = null;
		while(!(cmd=readCommand()).end){
			switch(cmd.cmdChar){
			case 'i':
				pq.insert(cmd.argument);
				break;
			case 'e':
				out.println(pq.extract());
				break;
			default:
			}
		}
		out.flush();
	}
	
	private CommandInfo readCommand() throws IOException {
		return CommandInfo.parse(in.br.readLine());
		
	}
}

class CommandInfo {
	public static CommandInfo parse(String cmd){
		CommandInfo c = new CommandInfo();
		if(cmd.charAt(2)=='d'){
			c.cmd="end";
			c.cmdChar='d';
			c.end=true;
		}else if(cmd.charAt(0)=='e'){
			c.cmd="extract";
			c.cmdChar='e';
		}else{
			c.cmd="insert";
			c.cmdChar='i';
			c.argument=Integer.parseInt(cmd.substring(7));
		}
		return c;
	}

	String cmd = null;
	char cmdChar = 0;
	int argument = -1;
	boolean end = false;
	
	private CommandInfo(){}
}

class BinaryHeap {
	protected int[] data = null;
	protected int dataPtr = -1;

	public BinaryHeap(int maxNodes) {
		data = new int[maxNodes+1];
		dataPtr = 1;
	}
	
	public int[] getAll() {
		return data;
	}
	
	public int get(int i) {
		if(i<=0 || data.length<=i) throw new IllegalArgumentException();
		return data[i];
	}
	
	public void set(int i, int value) {
		if(i<=0 || data.length<=i) throw new IllegalArgumentException();
		data[i] = value;
	}

	public int getRoot() {
		return get(1);
	}


	public void setRoot(int value) {
		set(1,value);
	}

	public int removeLast() {
		if(dataPtr <= 1) return -1;
		return data[--dataPtr];
	}
	
	public int addLast(int value) {
		if(dataPtr >= data.length) return -1;
		int ret = dataPtr++;
		data[ret] = value;
		return ret;
	}

	public int getParent(int i){
		if(i<=1 || data.length<=i) return -1;
		return i/2;
	}

	public int getLeft(int i){
		int leftId = i*2;
		return (i<=0 || data.length<=leftId) ? -1 : leftId;
	}

	public int getRight(int i){
		int rightId = i*2 + 1;
		return (i<=0 || data.length<=rightId) ? -1 :rightId;
	}
}

class MaximumHeap extends BinaryHeap {
	
	public MaximumHeap(int maxNodes) {
		super(maxNodes);
	}
	
	protected void maxHeapify(int i) {
		int leftId = getLeft(i);
		int rightId = getRight(i);
		int largest = i;
		if(leftId != -1 && data[largest] < data[leftId]) largest = leftId;
		if(rightId != -1 && data[largest] < data[rightId]) largest = rightId;
		if(largest != i){
			data[0] = data[i]; data[i] = data[largest]; data[largest] = data[0];
			maxHeapify(largest);
		}
	}
	
}

class PriorityQueue extends MaximumHeap {
	
	public PriorityQueue(int maxNodes) {
		super(maxNodes);
	}
	
	public void insert(int value) {
		for(int id = getParent(addLast(value)); id != -1; id = getParent(id)){
			maxHeapify(id);
		}
	}
	
	public int extract() {
		int ret = getRoot();
		setRoot(removeLast());
		maxHeapify(1);
		return ret;
	}
}

class InputUtil {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public String[] readStrArray(String delim) throws NumberFormatException, IOException{
    	return br.readLine().split(delim);
    }
    
    public int nextInt() throws NumberFormatException, IOException{
    	return Integer.parseInt(br.readLine());
    }
    
    public int[] nextInt(int[] a, int off, int num) throws NumberFormatException, IOException{
        int i=off;
        while(i<off+num) {
            for(String s: br.readLine().split(" ")){
            	if(i<off+num) {
                    a[i++] = Integer.parseInt(s);
            	}
            }
        }
        return a;
    }
}