Problem:
Switching Railroad Cars

This figure shows railway tracks for reshuffling cars. The rail tracks end in the bottom and the top-left rail track is used for the entrace and the top-right rail track is used for the exit. Ten cars, which have numbers from 1 to 10 respectively, use the rail tracks.

We can simulate the movement (comings and goings) of the cars as follow:

An entry of a car is represented by its number.

An exit of a car is represented by 0

For example, a sequence

1
6
0
8
10

demonstrates that car 1 and car 6 enter to the rail tracks in this order, car 6 exits from the rail tracks, and then car 8 and car 10 enter.

Write a program which simulates comings and goings of the cars which are represented by the sequence of car numbers. The program should read the sequence of car numbers and 0, and print numbers of cars which exit from the rail tracks in order. At the first, there are no cars on the rail tracks. You can assume that 0 will not be given when there is no car on the rail tracks.

Input

car number
car number or 0
car number or 0
.
.
.
car number or 0

The number of input lines is less than or equal to 100.

Output

For each 0, print the car number.

Sample Input

1
6
0
8
10
0
0
0

Output for the Sample Input

6
10
8
1

Solution in JAVA (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

public class Main {

	/** 車両を出す合図 */
	private static final String OUTPUT = "0";

	/**
	 * 入力値に従って車両を出し入れし、出ていく場合は車両番号を表示する。
	 *
	 * @param args
	 */
	public static void main(String[] args) throws IOException {

		Stack<String> stack = new Stack<String>();
		String line = null;
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));
		while ((line = reader.readLine()) != null) {

			if (OUTPUT.equals(line)) {
				// 車両を出す場合はスタック上の一番上にある車両を出力する
				System.out.println(stack.pop());

			} else {
				// スタックに車両を追加する
				stack.push(line);
			}
		}

		if (reader != null) {
			reader.close();
		}
	}

}