eolymp
bolt
Try our new interface for solving problems
Problems

Minimum in the Stack

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. \InputFile The input data will be generated in your program. You will be given some parameters to get the input sequence. The first input number \textbf{n} (\textbf{1} ≤ \textbf{n} ≤ \textbf{10^6}) is the number of operations with the stack. Then four nonnegative integers \textbf{a}, \textbf{b}, \textbf{c}, \textbf{x_0} are given. Their values are not greater than \textbf{10000}. Let's generate the input sequence \textbf{x}. The first number of input sequence is \textbf{x_1}. The first and each next number can be evaluated from the previous one using the formula: \textbf{x_i} = (\textbf{a·x^2_\{i-1\} + b·x_\{i-1\} + c}) / \textbf{100 mod 10^6}, where '\textbf{/}' is an integer division, '\textbf{mod}' is the remainder from division. If \textbf{x_i mod 5} < \textbf{2}, you must delete the number from the stack. Otherwise add number \textbf{x_i} to the stack. \OutputFile Print the resulting sum.
Time limit 1 second
Memory limit 122.17 MiB
Input example #1
2 0 0 1 81
Output example #1
0
Input example #2
3 1 1 1 13
Output example #2
0
Author В.Гольдштейн
Source Зимние сборы в Харькове 2010 День 2