#include <bits/stdc++.h>
using namespace std;
const int MAX = 1 << 19;
int a[MAX], b[MAX], n, k;
pair<long long, long long> calc(long long x) {
  long long cnt = 0, val = 0;
  priority_queue<long long> Qa, Qb;
  for (int i = (0); i < (n); i++) {
    Qa.push(-a[i]);
    long long stav = b[i] + x + (-Qa.top());
    long long prom = Qb.empty() ? (long long)(1e18) : b[i] - Qb.top();
    if (stav < prom && stav < 0) {
      cnt++, val += stav;
      Qa.pop();
      Qb.push(b[i]);
    } else if (prom < 0) {
      val += prom;
      Qb.pop();
      Qb.push(b[i]);
    }
  }
  return {cnt, val};
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = (0); i < (n); i++) scanf("%d", &a[i]);
  for (int i = (0); i < (n); i++) scanf("%d", &b[i]);
  long long lo = -1e12, hi = 1e12;
  for (; lo < hi;) {
    long long mid = (lo + hi) / 2;
    if (hi < 0) mid = (lo + hi - 1) / 2;
    if (calc(mid).first > k)
      lo = mid + 1;
    else
      hi = mid;
  }
  printf("%lld\n", calc(lo).second - lo * k);
  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;
const int MAX = 1 << 19;
int a[MAX], b[MAX], n, k;
pair<long long, long long> calc(long long x) {
  long long cnt = 0, val = 0;
  priority_queue<long long> Qa, Qb;
  for (int i = (0); i < (n); i++) {
    Qa.push(-a[i]);
    long long stav = b[i] + x + (-Qa.top());
    long long prom = Qb.empty() ? (long long)(1e18) : b[i] - Qb.top();
    if (stav < prom && stav < 0) {
      cnt++, val += stav;
      Qa.pop();
      Qb.push(b[i]);
    } else if (prom < 0) {
      val += prom;
      Qb.pop();
      Qb.push(b[i]);
    }
  }
  return {cnt, val};
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = (0); i < (n); i++) scanf("%d", &a[i]);
  for (int i = (0); i < (n); i++) scanf("%d", &b[i]);
  long long lo = -1e12, hi = 1e12;
  for (; lo < hi;) {
    long long mid = (lo + hi) / 2;
    if (hi < 0) mid = (lo + hi - 1) / 2;
    if (calc(mid).first > k)
      lo = mid + 1;
    else
      hi = mid;
  }
  printf("%lld\n", calc(lo).second - lo * k);
  return 0;
}

```