atcoder#CF17EXHIBITIONB. Increment and Swap

Increment and Swap

Score : 15001500 points

Problem Statement

We have a sequence AA of length NN.

On this sequence, we can perform the following two kinds of operations:

  • Swap two adjacent elements.
  • Select one element, and increment it by 11.

We will repeatedly perform these operations so that AA will be a non-decreasing sequence. Find the minimum required number of operations.

Constraints

  • 1N2000001 \leq N \leq 200000
  • 1Ai1091 \leq A_i \leq 10^9
  • AiA_i is an integer.

Input

Input is given from Standard Input in the following format:

NN

A1A_1

A2A_2

::

ANA_N

Output

Print the minimum number of operations required to turn AA into a non-decreasing sequence.

5
4
1
8
8
7
2

We can turn AA into a non-decreasing sequence in two operations:

  • Initially, A={4,1,8,8,7}A = \{4, 1, 8, 8, 7\}.
  • Swap the first two elements. Now, A={1,4,8,8,7}A = \{1, 4, 8, 8, 7\}.
  • Increment the last element by 11. Now, A={1,4,8,8,8}A = \{1, 4, 8, 8, 8\}.
20
8
2
9
7
4
6
7
9
7
4
7
4
4
3
6
2
3
4
4
9
62