#include <bits/stdc++.h>
using namespace std;
long long a[500010], b[500010];
long long inf = 1e13;
int main() {
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  int n = 0, k = 0;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%I64d", &a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%I64d", &b[i]);
  }
  long long l = 0, r = inf, res = 0;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    priority_queue<long long, vector<long long>, greater<long long>> qa;
    priority_queue<long long> qb;
    long long cur = 0, sz = 0;
    for (int i = 1; i <= n; ++i) {
      qa.push(a[i]);
      long long tmp1 = qa.top() + b[i] - mid;
      long long tmp2 = qb.empty() ? inf : (b[i] - qb.top());
      if (tmp1 <= tmp2 && tmp1 <= 0) {
        cur += tmp1;
        qa.pop();
        qb.push(b[i]);
        ++sz;
      } else if (tmp2 < tmp1 && tmp2 < 0) {
        cur += tmp2;
        qb.pop();
        qb.push(b[i]);
      }
    }
    if (sz >= k) {
      res = cur + k * mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  printf("%I64d", res);
  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;
long long a[500010], b[500010];
long long inf = 1e13;
int main() {
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  int n = 0, k = 0;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%I64d", &a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%I64d", &b[i]);
  }
  long long l = 0, r = inf, res = 0;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    priority_queue<long long, vector<long long>, greater<long long>> qa;
    priority_queue<long long> qb;
    long long cur = 0, sz = 0;
    for (int i = 1; i <= n; ++i) {
      qa.push(a[i]);
      long long tmp1 = qa.top() + b[i] - mid;
      long long tmp2 = qb.empty() ? inf : (b[i] - qb.top());
      if (tmp1 <= tmp2 && tmp1 <= 0) {
        cur += tmp1;
        qa.pop();
        qb.push(b[i]);
        ++sz;
      } else if (tmp2 < tmp1 && tmp2 < 0) {
        cur += tmp2;
        qb.pop();
        qb.push(b[i]);
      }
    }
    if (sz >= k) {
      res = cur + k * mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  printf("%I64d", res);
  return 0;
}

```