Minimum in the Queue#694

Minimum in the Queue

1 second
128 megabytes
Easy

The input to your program is a sequence of operations on a queue. Each operation is either adding or removing an element from the queue. After each operation, find the minimum among all numbers currently in the queue. Add up all such minimums to obtain the final answer. If after any operation the queue is empty, add nothing to the sum. If an attempt is made to remove an element when the queue is empty, do not perform the removal.

Input

The input will be generated directly in your program. Parameters will be given to generate the input sequence.

The first number (1 \leq n \leq 10^6) is the number of operations to perform on the queue. Then four non-negative integers , , , x_0 are given, each not exceeding .

To obtain the input sequence, generate the sequence .

The first number of the generated sequence is x_1. The first and each subsequent number is computed from the previous number as follows:

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

where denotes integer division and is the remainder after division.

If x_i \bmod 5 < 2, remove an element from the queue; otherwise, add x_i to the queue.

Output

Print the resulting sum.

Examples

Example 1

Loading
Loading

Example 5

Loading
Loading

Example 7

Loading
Loading