n = int(eval(input()))

l = list(map(int, input().split()))

s = sorted(l)

cost = 0

for i in range(n):

    a = l.index(s[i])

    j = 0

    while a > i:

        j += 1

        b = l.index(s[a])

        cost += l[b]

        l[a], l[b] = l[b], l[a]

        a = b

    cost += min(s[i] * j, s[i] * 2 + s[0] * (j + 2))

print(cost)

import itertools

 

 

WAIT, PRESORT, SORTED = 0, 1, 2

cost = 0

 

def swap(w1, w2):

    global cost

    cost += w1[WAIT] + w2[WAIT]

    w1[PRESORT], w2[PRESORT] = w2[PRESORT], w1[PRESORT]

     

 

n = eval(input())

w = [[wi, i, None] for i, wi in enumerate(map(int, input().split()))]

s = sorted(w)

minw = s[0]

for i, wi in enumerate (s):

    wi[SORTED] = i

     

for si in s:

    tmp = si

    t = []

    while tmp[SORTED] != si[PRESORT]:

        tmp = w[tmp[SORTED]]

        t += [tmp]

 

    if len(t ) and minw != si and si[WAIT] * (len(t) - 2) > minw[WAIT] * (len(t) + 2):

        swap(minw, si)

        for ti in t[::-1]:

            swap(minw, ti)

        swap(minw, si)

    else:

        for ti in t[::-1]:

            swap(si, ti)

     

print(cost)

n = int(eval(input()))
l = list(map(int, input().split()))
s = sorted(l)
cost = 0
for i in range(n):
    a = l.index(s[i])
    j = 0
    while a > i:
        j += 1
        b = l.index(s[a])
        cost += l[b]
        l[a], l[b] = l[b], l[a]
        a = b
    cost += min(s[i] * j, s[i] * 2 + s[0] * (j + 2))
print(cost)

import itertools

WAIT, PRESORT, SORTED = 0, 1, 2
cost = 0


def swap(w1, w2):
    global cost
    cost += w1[WAIT] + w2[WAIT]
    w1[PRESORT], w2[PRESORT] = w2[PRESORT], w1[PRESORT]


n = eval(input())
w = [[wi, i, None] for i, wi in enumerate(map(int, input().split()))]
s = sorted(w)
minw = s[0]
for i, wi in enumerate(s):
    wi[SORTED] = i
for si in s:
    tmp = si
    t = []
    while tmp[SORTED] != si[PRESORT]:
        tmp = w[tmp[SORTED]]
        t += [tmp]
    if len(t) and minw != si and si[WAIT] * (len(t) - 2) > minw[WAIT] * (len(t) + 2):
        swap(minw, si)
        for ti in t[::-1]:
            swap(minw, ti)
        swap(minw, si)
    else:
        for ti in t[::-1]:
            swap(si, ti)
print(cost)

-n = int(eval(input())) -l = list(map(int, input().split())) -s = sorted(l) +import itertools + +WAIT, PRESORT, SORTED = 0, 1, 2 -for i in range(n): -    a = l.index(s[i]) -    j = 0 -    while a > i: -        j += 1 -        b = l.index(s[a]) -        cost += l[b] -        l[a], l[b] = l[b], l[a] -        a = b -    cost += min(s[i] * j, s[i] * 2 + s[0] * (j + 2)) + + +def swap(w1, w2): +    global cost +    cost += w1[WAIT] + w2[WAIT] +    w1[PRESORT], w2[PRESORT] = w2[PRESORT], w1[PRESORT] + + +n = eval(input()) +w = [[wi, i, None] for i, wi in enumerate(map(int, input().split()))] +s = sorted(w) +minw = s[0] +for i, wi in enumerate(s): +    wi[SORTED] = i +for si in s: +    tmp = si +    t = [] +    while tmp[SORTED] != si[PRESORT]: +        tmp = w[tmp[SORTED]] +        t += [tmp] +    if len(t) and minw != si and si[WAIT] * (len(t) - 2) > minw[WAIT] * (len(t) + 2): +        swap(minw, si) +        for ti in t[::-1]: +            swap(minw, ti) +        swap(minw, si) +    else: +        for ti in t[::-1]: +            swap(si, ti)