cswork / OAOpen practice workspace

MicrosoftOA #30

Total Waiting Time for Handmade Items

Estimated HardJudge-ready

Topics (AI-labelled)Bits & MathPrefix & RangesIn-place Arrays

Each time, the employee works on the order at the front of the queue for 1 hour; if the order is finished it is delivered, otherwise it goes to the back of the queue. Compute the sum of every customer's waiting time from the start until they receive their order, modulo 10⁹.

Input

The first line contains N (1..100000), and the second line contains N preparation times (1..10000).

Output

Output a single integer, the answer.

Example 1

Input:

3
3 1 2

Output:

13

Example 2

Input:

4
1 2 3 4

Output:

24

Example 3

Input:

3
7 7 7

Output:

60

Example notes

Example 1: the completion times are 6, 2, 5, summing to 13.

Example 2: the completion times are 1, 5, 8, 10, summing to 24.

Example 3: after 6 full rounds totaling 18 hours, the orders finish at hours 19, 20, and 21 in turn, summing to 60.

Editorial

Loading solution…