// 최소 힙
package Silver_II_2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class Ex1927 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
int x = Integer.parseInt(br.readLine());
if (x == 0) {
if (maxHeap.isEmpty()) {
maxHeap.add(0);
}
System.out.println(maxHeap.poll());
} else {
maxHeap.add(x);
}
}
}
}