가장 작은/큰 원소를 뽑는 자료구조를 배워 봅시다.
Java / Python
최댓값을 빠르게 뽑는 자료구조를 배우는 문제

이번 문제는 최대 힙을 이용하여 연산을 하는 프로그램을 작성하는 문제입니다. 연산은 아래 두 종류 입니다.
1. 배열에 자연수 x를 넣는다.
2. 배열에서 가장 큰 값을 출력하고, 그 값을 배열에서 제거한다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.PriorityQueue;
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		// 최대 힙
		PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> o2 - o1);
		for (int i = 0; i < N; i++) {
			int num = Integer.parseInt(br.readLine());
			if (num == 0) {
				if (queue.isEmpty()) {
					System.out.println(0);
				} else {
					System.out.println(queue.poll());
				}
			} else {
				queue.offer(num);
			}
		}
	}
}
import sys
import heapq
N = int(sys.stdin.readline())
heap = []
# Max Heap
for _ in range(N):
    num = int(sys.stdin.readline())
    
    if num == 0:
        if heap:
            print(heapq.heappop(heap)[1])
        else:
            print("0")
        
    else:
        heapq.heappush(heap, [-num, num])