#include <bits/stdc++.h>
using namespace std;
const int Maxn = 500005;
int n, k, a[Maxn], b[Maxn];
long long ans;
priority_queue<pair<long long, bool> > Pr;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  long long lt = 0, rt = 2e9;
  while (true) {
    long long mid = (lt + rt) >> 1;
    int cnt = 0;
    ans = 0;
    for (int i = 1; i <= n; i++) {
      Pr.push(make_pair(-a[i], false));
      long long tmp = b[i] - mid - Pr.top().first;
      if (tmp < 0) ans += tmp, Pr.pop(), Pr.push(make_pair(b[i] - mid, true));
    }
    while (!Pr.empty()) cnt += Pr.top().second, Pr.pop();
    if (cnt == k) {
      printf("%lld", ans + k * mid);
      return 0;
    }
    if (cnt < k)
      lt = mid + 1;
    else
      rt = mid - 1;
  }
  return 0;
}

### Prompt

Please create a solution in Cpp 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 Maxn = 500005;
int n, k, a[Maxn], b[Maxn];
long long ans;
priority_queue<pair<long long, bool> > Pr;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  long long lt = 0, rt = 2e9;
  while (true) {
    long long mid = (lt + rt) >> 1;
    int cnt = 0;
    ans = 0;
    for (int i = 1; i <= n; i++) {
      Pr.push(make_pair(-a[i], false));
      long long tmp = b[i] - mid - Pr.top().first;
      if (tmp < 0) ans += tmp, Pr.pop(), Pr.push(make_pair(b[i] - mid, true));
    }
    while (!Pr.empty()) cnt += Pr.top().second, Pr.pop();
    if (cnt == k) {
      printf("%lld", ans + k * mid);
      return 0;
    }
    if (cnt < k)
      lt = mid + 1;
    else
      rt = mid - 1;
  }
  return 0;
}

```