#include <bits/stdc++.h>
using namespace std;
struct node {
  long long value;
  bool regret;
  bool operator<(const node &b) const { return value > b.value; }
};
int n, k;
long long a[500005], b[500005];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
  long long left = 0, right = 0x3f3f3f3f3f3f3f3fll;
  while (left <= right) {
    long long mid = (left + right) >> 1;
    int cnt = 0;
    long long tot = 0;
    priority_queue<node> q;
    for (int i = 1; i <= n; i++) {
      q.push((node){a[i] - mid, 0});
      long long temp = q.top().value + b[i];
      if (temp < 0) {
        tot += temp;
        if (q.top().regret == 0) cnt++;
        q.pop();
        q.push((node){-b[i], 1});
      }
    }
    if (cnt == k) {
      printf("%lld", tot + k * mid);
      return 0;
    }
    if (cnt > k)
      right = mid - 1;
    else
      left = mid + 1;
  }
  return 0;
}

### 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;
struct node {
  long long value;
  bool regret;
  bool operator<(const node &b) const { return value > b.value; }
};
int n, k;
long long a[500005], b[500005];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
  long long left = 0, right = 0x3f3f3f3f3f3f3f3fll;
  while (left <= right) {
    long long mid = (left + right) >> 1;
    int cnt = 0;
    long long tot = 0;
    priority_queue<node> q;
    for (int i = 1; i <= n; i++) {
      q.push((node){a[i] - mid, 0});
      long long temp = q.top().value + b[i];
      if (temp < 0) {
        tot += temp;
        if (q.top().regret == 0) cnt++;
        q.pop();
        q.push((node){-b[i], 1});
      }
    }
    if (cnt == k) {
      printf("%lld", tot + k * mid);
      return 0;
    }
    if (cnt > k)
      right = mid - 1;
    else
      left = mid + 1;
  }
  return 0;
}

```