import java.util.*;
public class Main {
static class Compare implements Comparator {
public int compare(Integer one, Integer two) {
return two.compareTo(one);
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Compare cmp = new Compare();
PriorityQueue q = new PriorityQueue(1, cmp);
int n = sc.nextInt();
while (n-- > 0) {
int x = sc.nextInt();
if (x == 0) {
if (q.isEmpty()) {
System.out.println(0);
} else {
System.out.println(q.poll());
}
} else {
q.offer(x);
}
}
}
}