eolymp
bolt
Try our new interface for solving problems
Problems

Minimum in the Queue

Minimum in the Queue

Time limit 1 second
Memory limit 128 MiB

The input to a program is a set of operations with the queue. Each operation is either addition or removal of an item to or from the queue. After each operation find the smallest among all the numbers in a queue. Add up all the resulting numbers and get an answer. If after any operation the queue is empty, then add nothing to the answer. If it is impossible to remove an item because the queue is empty, then do nothing.

Input data

The input data will be generated in your program. You will be given some parameters to get the input sequence.

The first input number n~(1 \le n \le 10^6) is the number of operations with the queue. Then four nonnegative integers a, b, c, x_0 are given. Their values are not greater than 10000.

Let's generate the input sequence x.

The first number of input sequence is x_1. The first and each next number can be evaluated from the previous number using the formula:

x_i = (a \cdot x_{i-1} \cdot x_{i-1} + b \cdot x_{i-1} + c) / 100~mod~10^6,

where "/" is an integer division, "mod" is the remainder from division.

If x_i~mod~5 < 2, you must delete the number from the queue, otherwise add number x_i to the queue.

Output data

Print the resulting sum.

Examples

Input example #1
2 0 0 1 81
Output example #1
0
Input example #5
7 2 1 176 36
Output example #5
60
Input example #7
9 5 6 777 30
Output example #7
2165995
Author В.Гольдштейн
Source Зимние сборы в Харькове 2010 День 2