#include<stack>
#include<cstdio>

using namespace std;

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