Minimum in the Stack
The input to a program is a set of operations with a stack. Each operation is either an addition or removal of an item to or from the stack. After each operation find the smallest number in a stack. Summarize all the resulting numbers and get the answer. If after any operation the stack is empty, then add nothing to the answer. If it is impossible to remove an item because the stack is empty, then do nothing.
Input
The input data will be generated in your program. You will be given some parameters to get the input sequence.
The first input number is the number of operations with the stack. Then four nonnegative integers are given. Their values are not greater than .
Let's generate the input sequence .
The first number of input sequence is . The first and each next number can be evaluated from the previous one using the formula:
where '' is an integer division, is the remainder from division.
If , you must delete the number from the stack. Otherwise add number to the stack.
Output
Print the resulting sum.