#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int n, k;
long long a[N], b[N];
priority_queue<pair<long long, long long> > q;
int main() {
  scanf("%d%d", &n, &k);
  for (register int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  for (register int i = 1; i <= n; i++) {
    scanf("%lld", &b[i]);
  }
  long long l = 0, r = 2000000009;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    int cnt = 0;
    long long tot = 0;
    for (register int i = 1; i <= n; i++) {
      q.push(make_pair(mid - a[i], 0));
      long long val = b[i] - q.top().first;
      if (val < 0) {
        tot += val;
        q.pop();
        q.push(make_pair(b[i], 1));
      }
    }
    while (!q.empty()) cnt += q.top().second, q.pop();
    if (cnt == k) {
      printf("%lld", tot + 1ll * k * mid);
      return 0;
    }
    if (cnt < k) l = mid + 1;
    if (cnt > k) r = mid - 1;
  }
  return 0;
}

### Prompt

Your task is to create a Cpp solution to 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>
using namespace std;
const int N = 500005;
int n, k;
long long a[N], b[N];
priority_queue<pair<long long, long long> > q;
int main() {
  scanf("%d%d", &n, &k);
  for (register int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  for (register int i = 1; i <= n; i++) {
    scanf("%lld", &b[i]);
  }
  long long l = 0, r = 2000000009;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    int cnt = 0;
    long long tot = 0;
    for (register int i = 1; i <= n; i++) {
      q.push(make_pair(mid - a[i], 0));
      long long val = b[i] - q.top().first;
      if (val < 0) {
        tot += val;
        q.pop();
        q.push(make_pair(b[i], 1));
      }
    }
    while (!q.empty()) cnt += q.top().second, q.pop();
    if (cnt == k) {
      printf("%lld", tot + 1ll * k * mid);
      return 0;
    }
    if (cnt < k) l = mid + 1;
    if (cnt > k) r = mid - 1;
  }
  return 0;
}

```