You are given $n$ integers $w_i (i = 0, 1, ..., n-1)$ to be sorted in ascending order. You can swap two integers $w_i$ and $w_j$. Each swap operation has a cost, which is the sum of the two integers $w_i + w_j$. You can perform the operations any number of times.

Write a program which reports the minimal total cost to sort the given integers.

Constraints

* $1 \leq n \leq 1,000$
* $0 \leq w_i\leq 10^4$
* $w_i$ are all different

Input

In the first line, an integer $n$ is given. In the second line, $n$ integers $w_i (i = 0, 1, 2, ... n-1)$ separated by space characters are given.

Output

Print the minimal cost in a line.

Examples

Input

5
1 5 3 4 2


Output

7


Input

4
4 3 2 1


Output

10
# -*- coding: utf-8 -*-
import copy

class BagsWithWeight(list):
    def __init__(self, l):
        super(BagsWithWeight, self).__init__(l)
        self.sorted_weights = list(sorted(self))

    def get_lists_of_loop(self):
        """???????????????????????????????????????????±??????????
        ????????\??????????????????????????????????????¬??\???????????? index ???????????????????????????
        ??????????????????????????¬??\???????????? index ????????£???????????¨?????????????????????????????????
        ?????????????????§?????????????????????????????¨ 1 ????????????????????????
        ????????????????????????????????? 1 ????????\???????????§??????????????????????????????????????????
        out: ListOfLoops     ??¨?????? loop ????????????
        """
        c_weights = copy.copy(self)
        loops = Loops()
        for i in range(len(self)):
            loop = []
            index = i
            while c_weights[index] != 0:
                loop.append(c_weights[index])
                c_weights[index] = 0
                index = self.get_index_of_sorted_bag(index)
            if loop:
                loops.append(loop)
        return loops

    def get_index_of_sorted_bag(self, index):
        """???????????????????????????????????????????????? index ????????????
        in:  index    int    ??????????????? index ????±?????????????????????? index
        out: int    ?????????????????????????????? index
        """
        return self.sorted_weights.index(self[index])


class Loops(list):
    def append(self, elm):
        elm.sort()
        elm = Loop(elm, self)
        super(Loops, self).append(elm)

    def get_minimum_element(self):
        return min(min(self))

    def get_cost(self):
        return sum(map((lambda loop: loop.get_cost_of_sorting()), self))


class Loop(list):
    def __init__(self, loop, loops):
        super(Loop, self).__init__(loop)
        self.loops = loops
    
    def get_minimum_weight_of_outer(self):
        return self.loops.get_minimum_element()

    def get_cost_of_sorting(self):
        """Loop ?????¨???????°??????????????±??????????
        """
        cost = sum(self) + min(self) * (len(self) - 2)
        cost_reduced = self.get_how_much_outer_minimum_weight_bag_reduces_cost()
        if cost_reduced > 0:
            return cost - cost_reduced
        else:
            return cost


    def get_how_much_outer_minimum_weight_bag_reduces_cost(self):
        """Loops ???????°????????????????????????£???????????§???????????????????±??????????
        List of Loops ??????????°?????????????????´????
        Loop ??????????°?????????????????´???¨??\????????????
        ?????????????????£?????????????????§???????????????????????????
        ??????????????? len(loop) - 1 ??????????????????????????????????°???????????????????????????????
        out: int    ????????§???????????????
        """
        return ((min(self) - self.get_minimum_weight_of_outer()) *
                    (len(self) - 1) -
                    (min(self) + self.get_minimum_weight_of_outer()) * 2)


def get_weights():
    raw_input()
    return map(int, raw_input().split())

bww = BagsWithWeight(get_weights())
loops = bww.get_lists_of_loop()
print loops.get_cost()