import           Control.Applicative
import           Control.Monad
import qualified Data.ByteString.Char8 as B

import           Data.IntMap.Lazy      (IntMap, (!))
import qualified Data.IntMap.Lazy      as M
import           Data.List
import           Data.Sequence         (Seq (..), ViewL (..), ViewR (..), (<|),
                                        (><), (|>))
import qualified Data.Sequence         as S

read' :: B.ByteString -> Int
read' bs | Just (n, _) <- B.readInt bs = n

getCycle :: (Int, Int)
         -> Seq Int    -- read only
         -> IntMap Int -- read only
         -> Seq Bool   -- update
         -> ([Int], Seq Bool)
getCycle (n, w) sa mb sc = go (n, w) [] sc
  where
    go (i, k) ws bs
        | bs `S.index` i = (ws, bs) -- already checked
        | otherwise      = go (i', k') (k:ws) bs'
      where
        bs' = S.update i True bs
        i' = mb ! k
        k' = sa `S.index` i'

getCycles :: [(Int, Int)]
          -> Seq Int    -- read only
          -> IntMap Int -- read only
          -> Seq Bool   -- update
          -> [[Int]]
getCycles [] _ _ _ = []
getCycles (w:ws) sa mb sc
    | null cs   = getCycles ws sa mb sc'
    | otherwise = cs : getCycles ws sa mb sc'
  where
    (cs, sc') = getCycle w sa mb sc

calcCost :: Int   -- minimum weight
         -> [Int] -- cycle
         -> Int
calcCost w cs = min (sumc + (n - 2) * mc) (sumc + mc + (n + 1) * w)
  where
    n = length cs
    mc = minimum cs
    sumc = sum cs

main :: IO ()
main = do
    n <- read' <$> B.getLine
    ws <- fmap read' . B.words <$> B.getLine

    let wseq = S.fromList ws
    let imap = M.fromList $ zip (sort ws) [0..]
    let bseq = S.replicate n False

    let minw = minimum ws
    let cycles = getCycles (zip [0..] ws) wseq imap bseq

    print $ sum $ fmap (calcCost minw) cycles

