#include <bits/stdc++.h>
inline long long read() {
  char c = getchar();
  long long ans = 0, tag = 1;
  for (; c < '0' || c > '9'; c = getchar()) tag = (c == '-' ? -1 : 1);
  for (; c >= '0' && c <= '9'; c = getchar()) (ans *= 10) += c - '0';
  return ans * tag;
}
using namespace std;
const long long maxn = 5e5 + 5, inf = 1e15;
long long n, k, a[maxn], b[maxn];
priority_queue<pair<long long, bool> > q;
int main() {
  n = read(), k = read();
  long long l = 1, r = 2e9, mid;
  for (long long i = 1; i <= n; i++) {
    a[i] = read();
  }
  for (long long i = 1; i <= n; i++) {
    b[i] = read();
  }
  while (l <= r) {
    mid = (l + r) >> 1;
    long long sum = 0, cnt = 0;
    for (long long i = 1, tmp; i <= n; i++) {
      q.push(make_pair(-a[i], 0));
      tmp = b[i] - q.top().first - mid;
      if (tmp < 0) {
        sum += tmp;
        q.pop();
        q.push(make_pair(b[i] - mid, 1));
      }
    }
    while (!q.empty()) {
      cnt += q.top().second;
      q.pop();
    }
    if (cnt == k) {
      printf("%lld", sum + k * mid);
      return 0;
    }
    if (cnt < k)
      l = mid + 1;
    else
      r = mid - 1;
  }
  return 0;
}

### Prompt

Please provide a CPP coded solution 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>
inline long long read() {
  char c = getchar();
  long long ans = 0, tag = 1;
  for (; c < '0' || c > '9'; c = getchar()) tag = (c == '-' ? -1 : 1);
  for (; c >= '0' && c <= '9'; c = getchar()) (ans *= 10) += c - '0';
  return ans * tag;
}
using namespace std;
const long long maxn = 5e5 + 5, inf = 1e15;
long long n, k, a[maxn], b[maxn];
priority_queue<pair<long long, bool> > q;
int main() {
  n = read(), k = read();
  long long l = 1, r = 2e9, mid;
  for (long long i = 1; i <= n; i++) {
    a[i] = read();
  }
  for (long long i = 1; i <= n; i++) {
    b[i] = read();
  }
  while (l <= r) {
    mid = (l + r) >> 1;
    long long sum = 0, cnt = 0;
    for (long long i = 1, tmp; i <= n; i++) {
      q.push(make_pair(-a[i], 0));
      tmp = b[i] - q.top().first - mid;
      if (tmp < 0) {
        sum += tmp;
        q.pop();
        q.push(make_pair(b[i] - mid, 1));
      }
    }
    while (!q.empty()) {
      cnt += q.top().second;
      q.pop();
    }
    if (cnt == k) {
      printf("%lld", sum + k * mid);
      return 0;
    }
    if (cnt < k)
      l = mid + 1;
    else
      r = mid - 1;
  }
  return 0;
}

```