#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int n, m, a[N], b[N];
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    q;
long long ans;
int solve(int x) {
  ans = 0;
  int num = 0;
  for (int i = (int)(1); i <= (int)(n); i++) {
    q.push(pair<long long, long long>(a[i], 0));
    long long t = q.top().first, now = b[i] - x;
    if (now + t < 0) {
      ans += now + t;
      q.pop();
      q.push(pair<long long, long long>(-now, 1));
    }
  }
  for (; !q.empty(); num += q.top().second, q.pop())
    ;
  return num;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &a[i]);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &b[i]);
  int l = 0, r = 2e9;
  while (l <= r) {
    int mid = (1ll * l + r) / 2;
    int num = solve(mid);
    if (num == m) return printf("%lld\n", ans + 1ll * m * mid), 0;
    num < m ? l = mid + 1 : r = mid - 1;
  }
}

### 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>
using namespace std;
const int N = 500005;
int n, m, a[N], b[N];
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    q;
long long ans;
int solve(int x) {
  ans = 0;
  int num = 0;
  for (int i = (int)(1); i <= (int)(n); i++) {
    q.push(pair<long long, long long>(a[i], 0));
    long long t = q.top().first, now = b[i] - x;
    if (now + t < 0) {
      ans += now + t;
      q.pop();
      q.push(pair<long long, long long>(-now, 1));
    }
  }
  for (; !q.empty(); num += q.top().second, q.pop())
    ;
  return num;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &a[i]);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &b[i]);
  int l = 0, r = 2e9;
  while (l <= r) {
    int mid = (1ll * l + r) / 2;
    int num = solve(mid);
    if (num == m) return printf("%lld\n", ans + 1ll * m * mid), 0;
    num < m ? l = mid + 1 : r = mid - 1;
  }
}

```