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
from heapq import heappush, heappop

cache = {}


def remaining_expected_cost(a):
    global n, b, cache
    key = tuple(i for i in range(n) if a[i] != b[i])
    if key in cache:
        return cache[key]
    return sum(a[i] for i in key)


def stack(node):
    global heap, n
    for i in range(n - 1):
        for j in range(i + 1, n):
            swapped = (i, j)
            if swapped == node[2]:
                continue
            a = node[3][:]
            w1, w2 = a[i], a[j] = a[j], a[i]
            spent_cost = node[1] + w1 + w2
            rec = remaining_expected_cost(a)
            if not rec:
                return spent_cost
            heappush(heap, (spent_cost + rec, spent_cost, swapped, a))
    return False


n, a = int(input()), list(map(int, input().split()))
b, m, heap = sorted(a), 0, []

heappush(heap, (remaining_expected_cost(a), 0, None, a))

while heap:
    m = stack(heappop(heap))
    if m is not False:
        break
print(m)