#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int n, m, cnt;
long long ans, sum, a[N], b[N];
priority_queue<pair<long long, bool> > q;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%I64d", &b[i]);
  long long l = 0, r = 2e9, mid;
  while (l <= r) {
    while (q.size()) q.pop();
    mid = (l + r) >> 1;
    sum = cnt = 0;
    for (int i = 1; i <= n; i++) {
      q.push(make_pair(mid - a[i], 0));
      if (b[i] - q.top().first < 0) {
        if (!q.top().second) cnt++;
        sum += b[i] - q.top().first;
        q.pop();
        q.push(make_pair(b[i], 1));
      }
    }
    if (cnt >= m)
      ans = sum + mid * m, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans;
  return 0;
}

### Prompt

Please provide a CPP coded solution to the problem described below:

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 = 500010;
int n, m, cnt;
long long ans, sum, a[N], b[N];
priority_queue<pair<long long, bool> > q;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%I64d", &b[i]);
  long long l = 0, r = 2e9, mid;
  while (l <= r) {
    while (q.size()) q.pop();
    mid = (l + r) >> 1;
    sum = cnt = 0;
    for (int i = 1; i <= n; i++) {
      q.push(make_pair(mid - a[i], 0));
      if (b[i] - q.top().first < 0) {
        if (!q.top().second) cnt++;
        sum += b[i] - q.top().first;
        q.pop();
        q.push(make_pair(b[i], 1));
      }
    }
    if (cnt >= m)
      ans = sum + mid * m, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans;
  return 0;
}

```