Solved.ac Class3++
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());
int size = Integer.parseInt(br.readLine());
for (int i = 0; i < size; i++) {
int data = Integer.parseInt(br.readLine());
if (data == 0) {
if (queue.isEmpty()) {
sb.append("0").append("\n");
} else {
sb.append(queue.remove()).append("\n");
}
} else {
queue.add(data);
}
}
System.out.println(sb);
}
}
성공
fun main() {
val sb = StringBuilder()
val priorityQueue = PriorityQueue<Int>(Comparator.reverseOrder())
val size = readln().toInt()
for (i in 0..<size) {
val data = readln().toInt()
if (data == 0) {
if (priorityQueue.isEmpty()) {
sb.append("0").append("\n")
} else {
sb.append(priorityQueue.remove()).append("\n")
}
} else {
priorityQueue.add(data)
}
}
print(sb)
}