#D3599. Minimum Cost Sort

    ID: 2991 Type: Default 1000ms 134MiB

Minimum Cost Sort

Minimum Cost Sort

You are given nn integers wi(i=0,1,...,n1)w_i (i = 0, 1, ..., n-1) to be sorted in ascending order. You can swap two integers wiw_i and wjw_j. Each swap operation has a cost, which is the sum of the two integers wi+wjw_i + w_j. You can perform the operations any number of times.

Write a program which reports the minimal total cost to sort the given integers.

Constraints

  • 1n1,0001 \leq n \leq 1,000
  • 0wi1040 \leq w_i\leq 10^4
  • wiw_i are all different

Input

In the first line, an integer nn is given. In the second line, nn integers wi(i=0,1,2,...n1)w_i (i = 0, 1, 2, ... n-1) separated by space characters are given.

Output

Print the minimal cost in a line.

Examples

Input

5 1 5 3 4 2

Output

7

Input

4 4 3 2 1

Output

10

inputFormat

Input

In the first line, an integer nn is given. In the second line, nn integers wi(i=0,1,2,...n1)w_i (i = 0, 1, 2, ... n-1) separated by space characters are given.

outputFormat

Output

Print the minimal cost in a line.

Examples

Input

5 1 5 3 4 2

Output

7

Input

4 4 3 2 1

Output

10

样例

4
4 3 2 1
10