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.

Constraints

* $1 \leq n \leq 1,000$
* $0 \leq w_i\leq 10^4$
* $w_i$ are all different

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.

Examples

Input

5
1 5 3 4 2


Output

7


Input

4
4 3 2 1


Output

10
using System;
using System.Linq;

namespace Sort_II
{
    class Minimum_Cost_Sort
    {
        static int[] sortindex;

        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;
            sortindex = new int[B.Length];
            for (int i = 0; i < sortindex.Length; i++)
                sortindex[i] = i;

            QuickSort(B, p, r);

            var cost = MinimumCost(A, B);

            Console.WriteLine(cost);
        }

        static public int MinimumCost(int[] A, int[] sorted)
        {
            var c = 0;
            var minIndex = -1;
            var min = 10000;
            for (int i = 0; i < A.Length; i++)
            {
                if (A[i] == sorted[i]) continue;
                else
                {
                    var j = sortindex[i];
                    if (A[i] == sorted[j])
                    {
                        c += A[i] + A[j];
                        var temp = A[i];
                        A[i] = A[j];
                        A[j] = temp;
                        i = 0;
                    }
                    else if (A[i] <=min) minIndex = i;
                }
            }
            while (minIndex != -1)
            {
                c += A[minIndex] + A[sortindex[minIndex]];
                var temp = A[minIndex];
                A[minIndex] = A[sortindex[minIndex]];
                A[sortindex[minIndex]] = temp;

                minIndex = -1;

                for (int i = 0; i < A.Length; i++)
                {
                    if (i == sortindex[i]) continue;
                    if (A[i] <= min) minIndex = i;
                }
            }

            return c;
        }

        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;
                    sortindex[i] = j;
                    sortindex[j] = i;
                }
            var tmp = A[i + 1];
            A[i + 1] = A[r];
            A[r] = tmp;
            sortindex[i + 1] = r;
            sortindex[r] = i + 1;

            return i + 1;
        }
    }
}