#D3599. Minimum Cost Sort
Minimum Cost Sort
Minimum Cost Sort
You are given integers to be sorted in ascending order. You can swap two integers and . Each swap operation has a cost, which is the sum of the two integers . You can perform the operations any number of times.
Write a program which reports the minimal total cost to sort the given integers.
Constraints
- are all different
Input
In the first line, an integer is given. In the second line, integers 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 is given. In the second line, integers 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