import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class 실험실 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder("");
PriorityQueue<Integer> pq = new PriorityQueue<Integer>((element1 , element2) -> {
if(Math.abs(element1) == Math.abs(element2)) {
return element1 < element2 ? -1 : 1;
}
return Math.abs(element1)-Math.abs(element2);
});
int N = Integer.parseInt(br.readLine());
int x;
while (N-- > 0) {
x = Integer.parseInt(br.readLine());
if (x != 0) {
pq.offer(x);
} else if(pq.isEmpty()){
sb.append("0").append("\n");
}else {
sb.append(pq.poll()).append("\n");
}
}
System.out.print(sb);
}
}