Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Minimum Cost Sort

You are given $n$ integers $w_i (i = 0, 1, ..., n-1)$ to be sorted in ascending order. You can swap two integers $w_i$ and $w_j$. Each swap operation has a cost, which is the sum of the two integers $w_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.

Input

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

Output

Print the minimal cost in a line.

Constraints

$1 \leq n \leq 1,000$

$0 \leq w_i\leq 10^4$

$w_i$ are all different

Sample Input 1

5
1 5 3 4 2

Sample Output 1

7

Sample Input 2

4
4 3 2 1

Sample Output 2

10

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

namespace Sort_II
{
    class Minimum_Cost_Sort
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
            var A = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
            var B = (int[])A.Clone();
            var p = 0;
            var r = B.Length - 1;
           
            QuickSort(B, p, r);

            var cost = MinimumCost(A,B);

            Console.WriteLine(cost);
        }

        static public int MinimumCost(int[] A,int[] sorted)
        {
            var min = 10001;
            var done = new bool[A.Length];
            var cycl = new Dictionary<int, int>();
            for (int i = 0; i < A.Length; i++)
            {
                cycl.Add(sorted[i],i);
                if (A[i] < min) min = A[i];
            }

            var cost = 0;

            for (int i = 0; i < A.Length; i++)
            {
                if (done[i]) continue;
                var index = i;
                var c = 0;
                var lmin = 10001;
                var sum = 0;
                while (true)
                {
                    if (done[index]) break;
                    c++;
                    done[index] = true;
                    if (A[index] < lmin) lmin = A[index];
                    sum += A[index];
                    index = cycl[A[index]];
                }
                var temp = sum - lmin * 2 + lmin * c;
                var alt = sum + lmin + min + min * c;
                if (alt < temp) cost += alt;
                else cost += temp;
            }

            return cost;
        }

        static public void QuickSort(int[] A, int p, int r)
        {
            if (p < r)
            {
                var q = Partition(A, p, r);
                QuickSort(A, p, q - 1);
                QuickSort(A, q + 1, r);
            }
        }

        static public int Partition(int[] A, int p, int r)
        {
            var x = A[r];
            var i = p - 1;
            for (int j = p; j < r; j++)
                if (A[j] <= x)
                {
                    i++;
                    var temp = A[i];
                    A[i] = A[j];
                    A[j] = temp;
                }
            var tmp = A[i + 1];
            A[i + 1] = A[r];
            A[r] = tmp;

            return i + 1;
        }
    }
}