import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
PriorityQueue<Integer> que = new PriorityQueue<>((a1,b1) ->
{
int first_abs = Math.abs(a1);
int second_abs = Math.abs(b1);
if (first_abs == second_abs) return a1 - b1;
return first_abs - second_abs;
}
);
for (int i = 0 ; i < N; i++)
{
int M = Integer.parseInt(br.readLine());
if (M==0)
{
if(que.size()==0)
{
System.out.println(0);
continue;
}
else{
int s = que.poll();
System.out.println(s);
continue;
}
}
que.add(M);
}
}
}
강의를 들으면서 큐를 설정할 때 큐가 절대값을 기준으로 음수 먼저 나와야 하므로 람다식을 이용하여 기본적으로 return first_abs - second_abs를 설정하였고 두 수의 절대값이 같고 부호만 다를 경우에는 a1-b1으로 설정하여 문제를 풀었다.