import java.util.*;
import java.io.*;
class EmptyStackException extends Exception {
	EmptyStackException(){
		 System.out.println("Stack is empty !");
	 }
}
class StackDemo extends Stack {
	public void top(StackDemo st) throws EmptyStackException{
		if (st.empty()==true){
			throw new EmptyStackException();
		}
		else {
			Integer a= (Integer) st.pop();
			System.out.println(a);
		}
	}
	
	public static void main(String[] args) {
		int total, number;
		StackDemo stack = new StackDemo();
		try{
			BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Enter number of elements you want to push");
			total=Integer.parseInt(br.readLine());
			if (total!=0){
				System.out.println("Enter the numbers");
				while (total>0){
					number=Integer.parseInt(br.readLine());
					stack.push(new Integer(number));
					total--;
				}
				System.out.println("The elemenet that the top the stack is ");
			}
			stack.top(stack);
		}
		catch(EmptyStackException se){
			System.out.println(se);
		}
		catch(IOException e){
			e.printStackTrace();
	    }
		catch(NumberFormatException e){
			System.out.println("Invalid value entered ");
		}
	}
}