#!/usr/bin/env python

# -*- coding: utf-8 -*-



import sys



MAX_VALUE = int(1e4 + 1)





def min_cost_sort():

    ans = 0

    for i in range(array_length):

        if check_order[i]:

            continue

        current_index = i

        circle_sum, circle_min, circle_size = 0, MAX_VALUE, 0

        while True:

            check_order[current_index] = True

            circle_size += 1

            current_value = array[current_index]

            circle_min = min(circle_min, current_value)

            circle_sum += current_value

            current_index = kv_reverse[current_value]

            if check_order[current_index]:

                break

        ans += min(circle_sum + (circle_size - 2) * circle_min,

                   circle_sum + circle_min + (circle_size + 1) * min_value)

    return ans





if __name__ == '__main__':

    _input = sys.stdin.readlines()

    array_length = int(_input[0])

    array = list(map(int, _input[1].split()))

    # assert len(array) == array_length



    check_order = [False] * array_length

    kv_reverse = [-1] * MAX_VALUE

    compare_ans = sorted(array)

    min_value = compare_ans[0]

    for j in range(array_length):

        kv_reverse[compare_ans[j]] = j

    print((min_cost_sort()))
#!/usr/bin/env python

# -*- coding: utf-8 -*-



import sys



MAX_VALUE = int(1e4 + 1)





def min_cost_sort():

    ans = 0

    for i in range(array_length):

        if check_order[i]:

            continue

        current_index = i

        circle_sum, circle_min, circle_size = 0, MAX_VALUE, 0

        while True:

            check_order[current_index] = True

            circle_size += 1

            current_value = array[current_index]

            circle_min = min(circle_min, current_value)

            circle_sum += current_value

            current_index = value_to_index[current_value]

            if check_order[current_index]:

                break

        ans += min(circle_sum + (circle_size - 2) * circle_min,

                   circle_sum + circle_min + (circle_size + 1) * min_value)

    return ans





if __name__ == '__main__':

    _input = sys.stdin.readlines()

    array_length = int(_input[0])

    array = list(map(int, _input[1].split()))

    # assert len(array) == array_length



    check_order = [False] * array_length

    value_to_index = dict()

    compare_ans = sorted(array)

    min_value = compare_ans[0]

    for j in range(array_length):

        value_to_index[compare_ans[j]] = j

    print((min_cost_sort()))
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys

MAX_VALUE = int(1e4 + 1)


def min_cost_sort():
    ans = 0
    for i in range(array_length):
        if check_order[i]:
            continue
        current_index = i
        circle_sum, circle_min, circle_size = 0, MAX_VALUE, 0
        while True:
            check_order[current_index] = True
            circle_size += 1
            current_value = array[current_index]
            circle_min = min(circle_min, current_value)
            circle_sum += current_value
            current_index = kv_reverse[current_value]
            if check_order[current_index]:
                break
        ans += min(
            circle_sum + (circle_size - 2) * circle_min,
            circle_sum + circle_min + (circle_size + 1) * min_value,
        )
    return ans


if __name__ == "__main__":
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1].split()))
    # assert len(array) == array_length
    check_order = [False] * array_length
    kv_reverse = [-1] * MAX_VALUE
    compare_ans = sorted(array)
    min_value = compare_ans[0]
    for j in range(array_length):
        kv_reverse[compare_ans[j]] = j
    print((min_cost_sort()))

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys

MAX_VALUE = int(1e4 + 1)


def min_cost_sort():
    ans = 0
    for i in range(array_length):
        if check_order[i]:
            continue
        current_index = i
        circle_sum, circle_min, circle_size = 0, MAX_VALUE, 0
        while True:
            check_order[current_index] = True
            circle_size += 1
            current_value = array[current_index]
            circle_min = min(circle_min, current_value)
            circle_sum += current_value
            current_index = value_to_index[current_value]
            if check_order[current_index]:
                break
        ans += min(
            circle_sum + (circle_size - 2) * circle_min,
            circle_sum + circle_min + (circle_size + 1) * min_value,
        )
    return ans


if __name__ == "__main__":
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1].split()))
    # assert len(array) == array_length
    check_order = [False] * array_length
    value_to_index = dict()
    compare_ans = sorted(array)
    min_value = compare_ans[0]
    for j in range(array_length):
        value_to_index[compare_ans[j]] = j
    print((min_cost_sort()))

-            current_index = kv_reverse[current_value] +            current_index = value_to_index[current_value] -    kv_reverse = [-1] * MAX_VALUE +    value_to_index = dict() -        kv_reverse[compare_ans[j]] = j +        value_to_index[compare_ans[j]] = j