- 우선순위 큐를 생성할때 정렬 기준을 설정해야 하는 문제.
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> {
int abs1 = Math.abs(o1);
int abs2 = Math.abs(o2);
if(abs1==abs2) {
if(o1 < o2 )
return -1;
else
return 1;
}
return abs1 - abs2;
});
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int n = sc.nextInt();
while(n-->0){
int cmd = sc.nextInt();
if(cmd == 0){
if(pq.isEmpty())
sb.append(0).append('\n');
else
sb.append(pq.poll()).append('\n');
}
else
pq.offer(cmd);
}
System.out.println(sb);
}
}