#K88452. Conveyor Belt Simulation

    ID: 37311 Type: Default 1000ms 256MiB

Conveyor Belt Simulation

Conveyor Belt Simulation

You are given a sequence of events representing the operation of a conveyor belt system in a warehouse. Each event is an integer. A nonzero integer represents the arrival of a package with that identification number, and a zero represents that one conveyor belt becomes available to process a package.

Whenever a conveyor belt becomes available (i.e. an event with the number 0 is encountered), the most recently arrived package that has not yet been processed is taken off the waiting stack and is processed immediately. This is a typical Last-In-First-Out (LIFO) operation. Formally, if we denote the sequence of events by \(e_1, e_2, \ldots, e_n\), then for each \(e_i = 0\) the package processed is the one corresponding to the most recent \(e_j \neq 0\) for which \(j < i\) and that has not already been processed.

Note: It is guaranteed that every time a 0 appears in the sequence, there is at least one package waiting to be processed.

For example, given the events:

6
5 7 0 3 0 0

The processed package order is: 7 3 5.

inputFormat

The first line of input contains an integer \(n\) (the total number of events). The second line contains \(n\) space-separated integers representing the events. A nonzero integer represents the arrival of a package with that ID, while a 0 represents an available conveyor belt that processes one package.

outputFormat

Output the identification numbers of the processed packages in the order they are processed. The numbers should be printed on a single line, separated by a single space.

## sample
6
5 7 0 3 0 0
7 3 5