"""Minimum cost Sort."""



def make_cycle(A):

    """Return a list which has the minimum element of A and

    switch elements cycles as list.



    A is a list whose elements' type is 'int'.



    Each cycle list has its minimum elements in the first index.

    """

    B = list(A)

    B.sort()

    cycle_list = [B[0]]

    for i, b in enumerate(B):

        if b != None:

            new_cycle = []

            new_cycle.append(b)

            B[i] = None

            si = A.index(b)

            while si != i:

                st = B[si]

                new_cycle.append(st)

                B[si] = None

                si = A.index(st)

            cycle_list.append(new_cycle)

    return cycle_list



def min_sorting_cost(A):

    """Return the minimum cost of sorting A.



    A is a list whose elements' type is 'int'.

    """

    cycles = make_cycle(A)

    min_w = cycles[0]

    cost = 0

    for c in cycles[1:]:

        n = len(c)

        min_cw = c[0]

        dec = (n - 1) * (min_cw - min_w)

        inc = 2 * (min_w + min_cw)

        if dec < inc:

            cost += sum(c) + min_cw * (n - 2)

        else:

            cost += sum(c) + min_cw * (n - 2) - dec + inc

    return cost





n = int(eval(input()))



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



ans = min_sorting_cost(A)



print(ans)
"""Minimum cost Sort."""



def min_cost_sort(A):

    """Sort list A in ascending order.

 

    And return the switching cost in sorting.

    """

    B = list(A)

    B.sort()

    cost = 0

    min_w = B[0]

    for i, b in enumerate(B):

        tmp_cost = 0

        bi = A.index(b)

        n = 1

        while bi != i:

            n += 1

            st = B[bi]

            si = A.index(st)

            tmp_cost += b + st

            A[bi], A[si] = st, b

            bi = si

        dec = (n - 1) * (b - min_w)

        inc = 2 * (min_w + b)

        if dec < inc:

            cost += tmp_cost

        else:

            cost += tmp_cost - dec + inc

    return cost





n = eval(input())



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



ans = min_cost_sort(A)



print(ans)
"""Minimum cost Sort."""


def make_cycle(A):
    """Return a list which has the minimum element of A and
    switch elements cycles as list.
    A is a list whose elements' type is 'int'.
    Each cycle list has its minimum elements in the first index.
    """
    B = list(A)
    B.sort()
    cycle_list = [B[0]]
    for i, b in enumerate(B):
        if b != None:
            new_cycle = []
            new_cycle.append(b)
            B[i] = None
            si = A.index(b)
            while si != i:
                st = B[si]
                new_cycle.append(st)
                B[si] = None
                si = A.index(st)
            cycle_list.append(new_cycle)
    return cycle_list


def min_sorting_cost(A):
    """Return the minimum cost of sorting A.
    A is a list whose elements' type is 'int'.
    """
    cycles = make_cycle(A)
    min_w = cycles[0]
    cost = 0
    for c in cycles[1:]:
        n = len(c)
        min_cw = c[0]
        dec = (n - 1) * (min_cw - min_w)
        inc = 2 * (min_w + min_cw)
        if dec < inc:
            cost += sum(c) + min_cw * (n - 2)
        else:
            cost += sum(c) + min_cw * (n - 2) - dec + inc
    return cost


n = int(eval(input()))
A = list(map(int, input().split()))
ans = min_sorting_cost(A)
print(ans)

"""Minimum cost Sort."""


def min_cost_sort(A):
    """Sort list A in ascending order.
    And return the switching cost in sorting.
    """
    B = list(A)
    B.sort()
    cost = 0
    min_w = B[0]
    for i, b in enumerate(B):
        tmp_cost = 0
        bi = A.index(b)
        n = 1
        while bi != i:
            n += 1
            st = B[bi]
            si = A.index(st)
            tmp_cost += b + st
            A[bi], A[si] = st, b
            bi = si
        dec = (n - 1) * (b - min_w)
        inc = 2 * (min_w + b)
        if dec < inc:
            cost += tmp_cost
        else:
            cost += tmp_cost - dec + inc
    return cost


n = eval(input())
A = list(map(int, input().split()))
ans = min_cost_sort(A)
print(ans)

-def make_cycle(A): -    """Return a list which has the minimum element of A and -    switch elements cycles as list. -    A is a list whose elements' type is 'int'. -    Each cycle list has its minimum elements in the first index. +def min_cost_sort(A): +    """Sort list A in ascending order. +    And return the switching cost in sorting. -    cycle_list = [B[0]] +    cost = 0 +    min_w = B[0] -        if b != None: -            new_cycle = [] -            new_cycle.append(b) -            B[i] = None -            si = A.index(b) -            while si != i: -                st = B[si] -                new_cycle.append(st) -                B[si] = None -                si = A.index(st) -            cycle_list.append(new_cycle) -    return cycle_list - - -def min_sorting_cost(A): -    """Return the minimum cost of sorting A. -    A is a list whose elements' type is 'int'. -    """ -    cycles = make_cycle(A) -    min_w = cycles[0] -    cost = 0 -    for c in cycles[1:]: -        n = len(c) -        min_cw = c[0] -        dec = (n - 1) * (min_cw - min_w) -        inc = 2 * (min_w + min_cw) +        tmp_cost = 0 +        bi = A.index(b) +        n = 1 +        while bi != i: +            n += 1 +            st = B[bi] +            si = A.index(st) +            tmp_cost += b + st +            A[bi], A[si] = st, b +            bi = si +        dec = (n - 1) * (b - min_w) +        inc = 2 * (min_w + b) -            cost += sum(c) + min_cw * (n - 2) +            cost += tmp_cost -            cost += sum(c) + min_cw * (n - 2) - dec + inc +            cost += tmp_cost - dec + inc -n = int(eval(input())) +n = eval(input()) -ans = min_sorting_cost(A) +ans = min_cost_sort(A)