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.util.Scanner;

public class Main{

	public static void main(String[] args) {
		
		Scanner in =  new Scanner (System.in);
		
		int numeros[] = new int[4];
		int cont = 0;
		
		while(in.hasNext()) {
			
			String toDo = in.next();
			
			if (toDo == "insert") {
				
				int inserir = in.nextInt();
				
				if(cont < numeros.length - 1) {
					numeros[cont] = inserir;
					cont++;
					heapSort(numeros, cont);
					
				}else if(cont == numeros.length) {
				
					numeros = biggerArray(numeros, inserir);
					cont++;
					heapSort(numeros, cont);
					
				}
			}else if (toDo == "extract") {
				System.out.println(numeros[0]);
				numeros = smallerArray(numeros);
				
			}else {
				
				break;
				
			}
		
			
		}
		

	}


	static int [] smallerArray (int[] numeros) {
		
		int array[] = new int [numeros.length-2];
		
		for (int i = 0; i < numeros.length -1; i++) {
			array[i] = numeros[i+1];
		}
		
		return array;
		
	}
	
	static int[] biggerArray(int[] numeros, int inserir) {
		int[] array = new int[numeros.length *2];
		for (int i = 0; i < array.length; i++) {
			if (i == array.length) {
				array[i] = inserir;
			}else {
				array[i] = numeros[i];
			}
		}

		return array;
	}

static void bottomUp(int a[], int num){
int k,v, j;

boolean checa;

for(int i = num/2; i >= 1; i--){
    k = i;

    checa = false;

    v = a[i];

    while(checa == false && (2*k) <= num){
        j = 2*k;
        if(j < num && a[j] < a[j+1]){

            j++;
        }
        if(v >= a[j]){

            checa = true;
        }
        else{

            a[k] = a[j];

            k = j;
        }
    }

    a[k] = v;
}
}

static int[] heapSort(int a[], int num){
if(num > 1){
    bottomUp(a, num);

    int aux = a[num];

    a[num] = a[1];

    a[1] = aux;

    heapSort(a, num-1);
}
return a;
}

}