#include <bits/stdc++.h>
const long long INF = 0x3F3F3F3F3F3FLL;
struct buf {
  operator int() {
    register int c = getchar(), x = 0;
    for (; !isdigit(c); c = getchar())
      ;
    for (; isdigit(c); c = getchar()) x = x * 10 - '0' + c;
    return x;
  }
} fio;
int n, k, i, s, t;
long long ans, l, r, m, u, v, a[500000], b[500000];
int main() {
  n = fio, k = fio;
  for (i = 0; i < n; ++i) a[i] = fio;
  for (i = 0; i < n; ++i) b[i] = fio;
  l = 1, r = 1000000000 * 2;
  while (l <= r) {
    m = (l + r) / 2;
    ans = s = t = 0;
    std::priority_queue<long long> P, Q;
    Q.push(-INF);
    for (i = 0; i < n; ++i) {
      P.push(m - a[i]);
      u = b[i] - P.top(), v = b[i] - Q.top();
      if (u < 0 && u < v) P.pop(), Q.push(b[i]), ans += u, ++s;
      if (v < 0 && v <= u) Q.pop(), Q.push(b[i]), ans += v;
    }
    while (P.size()) P.pop();
    while (Q.size()) Q.pop();
    Q.push(-INF);
    for (i = 0; i < n; ++i) {
      P.push(m - a[i]);
      u = b[i] - P.top(), v = b[i] - Q.top();
      if (u <= 0 && u <= v) P.pop(), Q.push(b[i]), ++t;
      if (v < 0 && v < u) Q.pop(), Q.push(b[i]);
    }
    if (s <= k && t >= k)
      break;
    else
      s > k ? r = m - 1 : l = m + 1;
  }
  printf(
      "%lld"
      "\n",
      ans + m * k);
  return 0;
}

### Prompt

In cpp, your task is to solve the following problem:

The plans for HC2 are rather far-fetched: we are just over 500 000 days away from HC2 3387, for example, and accordingly we are planning to have a couple hundred thousand problems in that edition (we hope that programming contests will become wildly more popular). The marmots need to get to work, and they could use a good plan...

Input

Same as the medium version, but the limits have changed: 1 ≤ k ≤ n ≤ 500 000.

Output

Same as the medium version.

Example

Input

8 4
3 8 7 9 9 4 6 8
2 5 9 4 3 8 9 1


Output

32

### Response

```cpp
#include <bits/stdc++.h>
const long long INF = 0x3F3F3F3F3F3FLL;
struct buf {
  operator int() {
    register int c = getchar(), x = 0;
    for (; !isdigit(c); c = getchar())
      ;
    for (; isdigit(c); c = getchar()) x = x * 10 - '0' + c;
    return x;
  }
} fio;
int n, k, i, s, t;
long long ans, l, r, m, u, v, a[500000], b[500000];
int main() {
  n = fio, k = fio;
  for (i = 0; i < n; ++i) a[i] = fio;
  for (i = 0; i < n; ++i) b[i] = fio;
  l = 1, r = 1000000000 * 2;
  while (l <= r) {
    m = (l + r) / 2;
    ans = s = t = 0;
    std::priority_queue<long long> P, Q;
    Q.push(-INF);
    for (i = 0; i < n; ++i) {
      P.push(m - a[i]);
      u = b[i] - P.top(), v = b[i] - Q.top();
      if (u < 0 && u < v) P.pop(), Q.push(b[i]), ans += u, ++s;
      if (v < 0 && v <= u) Q.pop(), Q.push(b[i]), ans += v;
    }
    while (P.size()) P.pop();
    while (Q.size()) Q.pop();
    Q.push(-INF);
    for (i = 0; i < n; ++i) {
      P.push(m - a[i]);
      u = b[i] - P.top(), v = b[i] - Q.top();
      if (u <= 0 && u <= v) P.pop(), Q.push(b[i]), ++t;
      if (v < 0 && v < u) Q.pop(), Q.push(b[i]);
    }
    if (s <= k && t >= k)
      break;
    else
      s > k ? r = m - 1 : l = m + 1;
  }
  printf(
      "%lld"
      "\n",
      ans + m * k);
  return 0;
}

```