#include <bits/stdc++.h>
using namespace std;
int read() {
  int X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar();
  return X * w;
}
const int N = 500000 + 10;
int n, k, a[N], b[N];
priority_queue<pair<long long, int>> Q;
pair<long long, int> calc(long long mid) {
  long long s = 0;
  int c = 0;
  for (int i = 1; i <= n; ++i) {
    Q.emplace(-a[i], 0);
    long long r = (long long)b[i] - mid - Q.top().first;
    if (r < 0) s += r, Q.pop(), Q.emplace(b[i] - mid, 1);
  }
  while (!Q.empty()) c += Q.top().second, Q.pop();
  return make_pair(s, c);
}
int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; ++i) a[i] = read();
  for (int i = 1; i <= n; ++i) b[i] = read();
  long long L = 0, R = 2e9;
  while (L < R) {
    long long mid = (L + R + 1) >> 1;
    if (calc(mid).second <= k)
      L = mid;
    else
      R = mid - 1;
  }
  printf("%lld\n", calc(L).first + 1ll * L * k);
  return 0;
}

### Prompt

Develop a solution in cpp 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;
int read() {
  int X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar();
  return X * w;
}
const int N = 500000 + 10;
int n, k, a[N], b[N];
priority_queue<pair<long long, int>> Q;
pair<long long, int> calc(long long mid) {
  long long s = 0;
  int c = 0;
  for (int i = 1; i <= n; ++i) {
    Q.emplace(-a[i], 0);
    long long r = (long long)b[i] - mid - Q.top().first;
    if (r < 0) s += r, Q.pop(), Q.emplace(b[i] - mid, 1);
  }
  while (!Q.empty()) c += Q.top().second, Q.pop();
  return make_pair(s, c);
}
int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; ++i) a[i] = read();
  for (int i = 1; i <= n; ++i) b[i] = read();
  long long L = 0, R = 2e9;
  while (L < R) {
    long long mid = (L + R + 1) >> 1;
    if (calc(mid).second <= k)
      L = mid;
    else
      R = mid - 1;
  }
  printf("%lld\n", calc(L).first + 1ll * L * k);
  return 0;
}

```