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.
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:
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.
Print the resulting sum.
Example 1
Example 5
Example 7