using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ALDS1_6_D
{
    class Program
    {
        private static int n;
        private static int[] W;
        private static Tuple[] sortedW;
        static void Main(string[] args)
        {
            Program.n = int.Parse(Console.ReadLine());
            Program.W = Console.ReadLine()
                               .Split(' ')
                               .Select(i => int.Parse(i))
                               .ToArray();
            int[] tmpW = new int[n];

            for (int i = 0; i < n; i++)
            {
                tmpW[i] = Program.W[i];
            }

            Program.sortedW = new Tuple[Program.n];
            for (int i = 0; i < tmpW.Length; i++)
            {
                sortedW[i] = new Tuple(tmpW[i], i);
            }

            Program.MergeSort(0, Program.sortedW.Length - 1);

            List<List<int>> grpList = new List<List<int>>();

            Tuple tuple;
            
            for (int i = 0; i < n; i++)
            {
                tuple = Program.sortedW[i];
                if (!tuple.isChecked)
                {
                    List<int> tmpGrp = new List<int>();
                    tmpGrp.Add(tuple.num);
                    tuple.isChecked = true;
                    while (tuple.originIndx != i)
                    {
                        tuple = Program.sortedW[tuple.originIndx];
                        tmpGrp.Add(tuple.num);
                        tuple.isChecked = true;
                    }
                    grpList.Add(tmpGrp);
                }
            }

            int totalWeight = 0;

            foreach (List<int> list in grpList)
            {
                totalWeight += Math.Min((list.Count-2)*list.Min() + list.Sum()
                                        , (list.Count+1)*Program.W.Min() + list.Sum() + list.Min());
            }

            Console.WriteLine(totalWeight);
        }

        private static void MergeSort(int leftIndx, int rightIndx)
        {
            if(leftIndx == rightIndx)
            {
                return;
            }

            int midIndx = (leftIndx + rightIndx) / 2;

            Program.MergeSort(leftIndx, midIndx);
            Program.MergeSort(midIndx + 1, rightIndx);
            Program.Merge(leftIndx, midIndx, rightIndx);

        }

        private static void Merge(int leftIndx, int midIndx, int rightIndx)
        {
            Tuple[] tmpL = new Tuple[midIndx - leftIndx + 1];
            Tuple[] tmpR = new Tuple[rightIndx - midIndx];

            for (int i = 0; i < tmpL.Length; i++)
            {
                tmpL[i] = Program.sortedW[leftIndx + i];
            }

            for (int i = 0; i < tmpR.Length; i++)
            {
                tmpR[i] = Program.sortedW[midIndx + 1 + i];
            }

            int tmpLIndx = 0;
            int tmpRIndx = 0;

            for (int i = 0; i < rightIndx - leftIndx + 1; i++)
            {
                if (tmpLIndx < tmpL.Length && tmpRIndx < tmpR.Length && tmpL[tmpLIndx].IsSmaller(tmpR[tmpRIndx]) || tmpRIndx >= tmpR.Length)
                {
                    Program.sortedW[leftIndx + i] = tmpL[tmpLIndx];
                    tmpLIndx++;
                }
                else
                {
                    Program.sortedW[leftIndx + i] = tmpR[tmpRIndx];
                    tmpRIndx++;
                }
            }
        }
    }

    class Tuple
    {
        public int num { private set; get; }
        public int originIndx { private set; get; }

        public bool isChecked = false;
        public Tuple(int num, int originIndx)
        {
            this.num = num;
            this.originIndx = originIndx;
        }

        public bool IsSmaller(Tuple target)
        {
            if (target == null)
            {
                return false;
            }

            return this.num <= target.num;
        }
    }


}



Measured memory_bytes: 20392