#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c = getchar();
  long long x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
long long n, k, a[500005], b[500005];
long long sum, cnt;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    q;
void check(long long mid) {
  sum = cnt = 0;
  for (register long long i = (1); i <= (n); ++i) {
    q.push(make_pair(a[i], 0));
    long long tmp = b[i] + q.top().first - mid;
    if (tmp < 0) sum += tmp, q.pop(), q.push(make_pair(mid - b[i], 1));
  }
  while (!q.empty()) cnt += q.top().second, q.pop();
}
signed main() {
  n = read(), k = read();
  for (register long long i = (1); i <= (n); ++i) a[i] = read();
  for (register long long i = (1); i <= (n); ++i) b[i] = read();
  long long l = 0, r = 2e9;
  while (l <= r) {
    long long mid = l + r >> 1;
    check(mid);
    if (cnt == k) return printf("%lld", sum + k * mid) & 0;
    if (cnt < k)
      l = mid + 1;
    else
      r = mid - 1;
  }
  puts("qaq");
  return 0;
}

### Prompt

Your challenge is to write 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;
inline long long read() {
  char c = getchar();
  long long x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
long long n, k, a[500005], b[500005];
long long sum, cnt;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    q;
void check(long long mid) {
  sum = cnt = 0;
  for (register long long i = (1); i <= (n); ++i) {
    q.push(make_pair(a[i], 0));
    long long tmp = b[i] + q.top().first - mid;
    if (tmp < 0) sum += tmp, q.pop(), q.push(make_pair(mid - b[i], 1));
  }
  while (!q.empty()) cnt += q.top().second, q.pop();
}
signed main() {
  n = read(), k = read();
  for (register long long i = (1); i <= (n); ++i) a[i] = read();
  for (register long long i = (1); i <= (n); ++i) b[i] = read();
  long long l = 0, r = 2e9;
  while (l <= r) {
    long long mid = l + r >> 1;
    check(mid);
    if (cnt == k) return printf("%lld", sum + k * mid) & 0;
    if (cnt < k)
      l = mid + 1;
    else
      r = mid - 1;
  }
  puts("qaq");
  return 0;
}

```