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;
int w[131072], sum[131072], n, k, l, r, mid;
char c;
bool is_digit() { return c <= '9' && c >= '0'; }
void read(int &a) {
  a = 0;
  while ((c = getchar()) && (is_digit())) a = a * 10 + c - '0';
}
bool pc(int a) {
  memset(sum, 0, sizeof(sum));
  int j = 0;
  for (int i = 0, t = 0; i < n; i++) {
    if (t + w[i] > a)
      j++, t = w[i];
    else
      t += w[i];
  }
  return j < k;
}
int main() {
  read(n);
  read(k);
  for (int i = 0; i < n; i++) {
    read(w[i]);
    l = max(w[i], l);
    r += w[i];
  }
  while (l != r) {
    mid = (l + r) / 2;
    if (pc(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << "\n";
  return 0;
}
