import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PriorityQueue<Integer> queue = new PriorityQueue<>();
int N = Integer.parseInt(br.readLine()); // 형 변환
for (int i = 0; i < N; i++) {
int val = Integer.parseInt(br.readLine()); // 형 변환
if (val == 0) {
if (queue.isEmpty()) {
System.out.println("0");
} else {
System.out.println(queue.poll());
}
} else {
queue.add(val);
}
}
}
}
PriorityQueue : 우선순위 큐