import java.io.*;
import java.util.*;
public class Main {
public static int N;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
PriorityQueue<Integer> pq = new PriorityQueue<>((a,b) -> {
if (Math.abs(a) < Math.abs(b)) return -1;
else if (Math.abs(a) > Math.abs(b)) return 1;
else {
return a.compareTo(b);
}
});
StringBuilder sb = new StringBuilder();
for (int loop = 0; loop < N; ++loop) {
int num = Integer.parseInt(br.readLine());
if (num == 0) {
int result = pq.isEmpty() ? 0 : pq.poll();
sb.append(result + "\n");
} else {
pq.add(num);
}
}
System.out.println(sb);
}
}