#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
int N, K;
int a[500009], b[500009];
long long int sol, ans = 0;
int solve(long long int mid) {
  int cnt = 0;
  sol = 0;
  priority_queue<pair<long, int> > pqp;
  for (int i = 0; i < N; ++i) {
    pqp.push({-a[i], 0});
    long long int u = b[i] - mid, v = pqp.top().first;
    if (u < v) {
      sol += u - v;
      pqp.pop();
      pqp.push({u, 1});
    }
  }
  while (!pqp.empty()) {
    cnt += pqp.top().second;
    pqp.pop();
  }
  return cnt;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> K;
  for (int i = 0; i < N; ++i) cin >> a[i];
  for (int i = 0; i < N; ++i) cin >> b[i];
  long long int l = 0, r = 2000000000LL;
  while (l <= r) {
    long long int mid = (l + r) >> 1;
    if (solve(mid) <= K) {
      l = mid + 1;
      ans = sol + mid * K;
    } else {
      r = mid - 1;
    }
  }
  printf("%lld\n", ans);
}

### Prompt

Please formulate 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 long long int MOD = 1000000007;
int N, K;
int a[500009], b[500009];
long long int sol, ans = 0;
int solve(long long int mid) {
  int cnt = 0;
  sol = 0;
  priority_queue<pair<long, int> > pqp;
  for (int i = 0; i < N; ++i) {
    pqp.push({-a[i], 0});
    long long int u = b[i] - mid, v = pqp.top().first;
    if (u < v) {
      sol += u - v;
      pqp.pop();
      pqp.push({u, 1});
    }
  }
  while (!pqp.empty()) {
    cnt += pqp.top().second;
    pqp.pop();
  }
  return cnt;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> K;
  for (int i = 0; i < N; ++i) cin >> a[i];
  for (int i = 0; i < N; ++i) cin >> b[i];
  long long int l = 0, r = 2000000000LL;
  while (l <= r) {
    long long int mid = (l + r) >> 1;
    if (solve(mid) <= K) {
      l = mid + 1;
      ans = sol + mid * K;
    } else {
      r = mid - 1;
    }
  }
  printf("%lld\n", ans);
}

```