#include <iostream>
#include <queue>
#include<cstdlib>
using namespace std;
class cmp
{
public:
bool operator()(int a, int b)
{
if (abs(a) < abs (b))
return false;
else if (abs(a)==abs(b) && a<=b)
return false;
else
return true;
}
};
int main()
{
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
priority_queue<int, vector<int>, cmp > pq;
int input;
cin>>N;
for (int i = 0; i < N; i++)
{
cin>>input;
if (input==0)
{
if(pq.empty())
{
cout<<0<<'\n';
}
else
{
cout<<pq.top()<<'\n';
pq.pop();
}
}
else
{
pq.push(input);
}
}
return 0;
}