#K89287. Validate and Correct Stack Operations

    ID: 37497 Type: Default 1000ms 256MiB

Validate and Correct Stack Operations

Validate and Correct Stack Operations

You are given a sequence of operations representing a stack manipulation. The operations include push x, pop, and inc k x. The inc operation increases each of the bottom k elements of the stack by x. If an operation is invalid (for example, a pop on an empty stack or an inc when there are fewer than k elements), you must insert the minimum number of push operations (with the value 1) before that operation so that the sequence becomes valid.

Formally, if at any point a pop operation is encountered while the stack is empty, you must first insert a push 1 operation to correct it. Similarly, if an inc k x operation is encountered and the current stack size is less than k, you must insert exactly k - |stack| push 1 operations before the inc operation. The goal is to determine the minimum number of push operations inserted and produce the corrected sequence of operations.

Note that the formula used to determine the number of pushes needed when processing an inc operation is: $$\text{missing pushes} = k - |stack|$$ where \(|stack|\) is the current size of the stack.

inputFormat

The input is provided via stdin and has the following format:

  1. The first line contains an integer n representing the number of operations in the sequence.
  2. The next n lines each contain a string describing an operation. Each operation is one of the following forms:
    • push x where x is an integer (1 ≤ x ≤ 109).
    • pop
    • inc k x where k (1 ≤ k ≤ current stack size) and x are integers.

outputFormat

Output the result to stdout with the following format:

  1. The first line is an integer representing the minimum number of additional push operations inserted.
  2. Each subsequent line prints one operation from the corrected sequence, in the order they are executed.
## sample
3
push 1
pop
push 2
0

push 1 pop push 2

</p>