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
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e18;
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& o) {
  out << "(" << o.first << "," << o.second << ")";
  return out;
}
template <class T>
ostream& operator<<(ostream& out, const vector<T> V) {
  for (int i = 0; i < V.size(); i++) {
    out << V[i];
    if (i != V.size() - 1) out << " ";
  }
  return out;
}
template <class T>
ostream& operator<<(ostream& out, const vector<vector<T> > Mat) {
  for (int i = 0; i < Mat.size(); i++) {
    if (i != 0) out << endl;
    out << Mat[i];
  }
  return out;
}
template <class S, class T>
ostream& operator<<(ostream& out, const map<S, T> mp) {
  out << "{ ";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    out << it->first << ":" << it->second;
    if (mp.size() - 1 != distance(mp.begin(), it)) out << ", ";
  }
  out << " }";
  return out;
}
long long solve() {
  long long res = 0;
  long long n;
  cin >> n;
  vector<long long> w(n);
  for (auto& in : w) cin >> in;
  auto tmp_w = w;
  sort(tmp_w.begin(), tmp_w.end());
  for (int i = 0; i < n; i++) {
    if (tmp_w[i] == w[i]) continue;
    res += tmp_w[i] + w[i];
  }
  return res / 2;
}
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << solve() << endl;
  return 0;
}
