#K35482. Stack Operations Simulation

    ID: 25541 Type: Default 1000ms 256MiB

Stack Operations Simulation

Stack Operations Simulation

You are given a sequence of integers representing operations on a stack. The operations are defined as follows:

  • If the integer x is positive, push x onto the stack.
  • If x = 0, duplicate the top element of the stack. That is, if the stack is non-empty and its top element is t, then a 0 operation pushes another t onto the stack.
  • If x is negative, let \( k = -x \). Then pop the top \( k \) elements from the stack. This operation is valid only if the stack currently contains at least \( k \) elements.

If any operation is invalid (i.e. a duplicate (0) on an empty stack or a pop request when there aren’t enough elements), the entire process is considered invalid and the result should be an empty stack.

Your task is to simulate these operations and output the final state of the stack. If the final stack is non-empty, output its elements in order (from bottom to top) separated by a space. If the stack is empty, output nothing.

inputFormat

The input is given via standard input (stdin) and has the following format:

N
a1 a2 a3 ... aN

Where:

  • N is an integer indicating the number of operations.
  • The second line contains N space-separated integers representing the operations.

outputFormat

Print the final state of the stack to standard output (stdout) as a sequence of space-separated integers on a single line. If the final stack is empty, print nothing.

## sample
6
5 7 0 -2 3 -3