#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
const long long N = 5e5 + 5;
long long n, k, L = 0, R = 2e9, a[N], b[N];
struct node {
  long long first, second;
};
bool operator<(node a, node b) { return a.first > b.first; }
signed main() {
  n = read(), k = read();
  for (long long i = (1); i <= (n); ++i) a[i] = read();
  for (long long i = (1); i <= (n); ++i) b[i] = read();
  while (L <= R) {
    long long mid = (L + R) >> 1, cnt = 0, tot = 0;
    priority_queue<node> q;
    for (long long i = (1); i <= (n); ++i) {
      q.push({a[i] - mid, 0});
      long long val = b[i] + q.top().first;
      if (val < 0) {
        tot += val;
        if (q.top().second == 0) cnt++;
        q.pop();
        q.push({-b[i], 1});
      }
    }
    if (cnt == k) {
      cout << tot + k * mid << endl;
      return 0;
    } else if (cnt < k)
      L = mid + 1;
    else
      R = mid - 1;
  }
  return 0;
}

### Prompt

Construct a CPP code solution to the problem outlined:

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>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
const long long N = 5e5 + 5;
long long n, k, L = 0, R = 2e9, a[N], b[N];
struct node {
  long long first, second;
};
bool operator<(node a, node b) { return a.first > b.first; }
signed main() {
  n = read(), k = read();
  for (long long i = (1); i <= (n); ++i) a[i] = read();
  for (long long i = (1); i <= (n); ++i) b[i] = read();
  while (L <= R) {
    long long mid = (L + R) >> 1, cnt = 0, tot = 0;
    priority_queue<node> q;
    for (long long i = (1); i <= (n); ++i) {
      q.push({a[i] - mid, 0});
      long long val = b[i] + q.top().first;
      if (val < 0) {
        tot += val;
        if (q.top().second == 0) cnt++;
        q.pop();
        q.push({-b[i], 1});
      }
    }
    if (cnt == k) {
      cout << tot + k * mid << endl;
      return 0;
    } else if (cnt < k)
      L = mid + 1;
    else
      R = mid - 1;
  }
  return 0;
}

```