#include<stack>
#include<cstdio>
#include<queue>

using namespace std;

int main()
{
	stack<int> trains;
	queue<int> aparted;
	int n, c, i;
	scanf("%d", &n);
	for (i = 0; i < n; i++)
	{
		scanf("%d", &c);
		if (c == 0)
		{
			aparted.push(trains.top());
			trains.pop();
		}
		else
			trains.push(c);
	}
	while (aparted.empty()!=true)
	{
		printf("%d\n", aparted.front());
		aparted.pop();
	}
	return 0;
}