// Arup Guha
// 10/28/2026
// Task class to use for Priority Queue example
/*** Edited on 11/4/2026 to show file input. 
     This version crashes when you give it a file that doesn't exist.
***/

import java.util.*;
import java.io.*;

public class whattask_file2 {

	public static void main(String[] args) {
		
		// Get the file name
		Scanner stdin = new Scanner(System.in);
		
		// Set up to read from file.
		Scanner fin = null;
		
		while (true) {
			System.out.println("What file has the input?");
			String filename = stdin.next();
			
			// Try opening the file.	
			try {
				fin = new Scanner(new File(filename));
				break;
			}
			catch (FileNotFoundException e) {
				System.out.println("File not found. Please try again.");
			}
			
		}
		int numRequests = fin.nextInt();
		
		// PQ for our jobs
		PriorityQueue<Task> myPQ = new PriorityQueue<Task>();
		
		// Process requests.
		for (int loop=0; loop<numRequests; loop++) {
		
			int cmd = fin.nextInt();
			
			// Ugh more work...
			if (cmd == 1) {
				
				// Read in info, create object add to PQ.
				String myTask = fin.next();
				int myMoney = fin.nextInt();
				myPQ.add(new Task(myTask, myMoney));
			}
			
			// I'll actually do something and print it out...
			else {
				System.out.println(myPQ.poll());
			}
		}
		
		// Close the file.
		fin.close();
	}
}