#include <bits/stdc++.h>
int n, k, a[500010], b[500010];
long long calc(long long M, int& t) {
  std::priority_queue<int> A, B;
  long long s = 0, x, y;
  for (int i = t = 0; i < n; i++) {
    A.push(-a[i]);
    x = -A.top() - M + b[i];
    y = B.empty() ? 0 : b[i] - B.top();
    if (x < y) {
      if (x < 0) s += x, t++, A.pop(), B.push(b[i]);
    } else if (y < 0)
      s += y, B.pop(), B.push(b[i]);
  }
  return s + k * M;
}
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 left = 0, right = 1ll << 32, mid;
  int t;
  while (right - left > 1) {
    calc(mid = left + right >> 1, t);
    if (t <= k)
      left = mid;
    else
      right = mid;
  }
  printf("%lld\n", calc(left, t));
}

### 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>
int n, k, a[500010], b[500010];
long long calc(long long M, int& t) {
  std::priority_queue<int> A, B;
  long long s = 0, x, y;
  for (int i = t = 0; i < n; i++) {
    A.push(-a[i]);
    x = -A.top() - M + b[i];
    y = B.empty() ? 0 : b[i] - B.top();
    if (x < y) {
      if (x < 0) s += x, t++, A.pop(), B.push(b[i]);
    } else if (y < 0)
      s += y, B.pop(), B.push(b[i]);
  }
  return s + k * M;
}
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 left = 0, right = 1ll << 32, mid;
  int t;
  while (right - left > 1) {
    calc(mid = left + right >> 1, t);
    if (t <= k)
      left = mid;
    else
      right = mid;
  }
  printf("%lld\n", calc(left, t));
}

```