#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long w = 1, s = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return w * s;
}
long long n, k;
long long A[1001010], B[1001000];
priority_queue<long long, vector<long long>, greater<long long> > q1, q2;
inline bool check(long long mid) {
  while (!q1.empty()) q1.pop();
  while (!q2.empty()) q2.pop();
  long long Ans = 0, tot = 0;
  for (register long long i = 1; i <= n; ++i) {
    q1.push(A[i]);
    long long x1 = q1.top() + B[i] - mid,
              x2 = q2.size() ? q2.top() + B[i] : 1e18;
    if (x1 <= x2 && x1 <= 0) {
      Ans += x1;
      ++tot;
      q1.pop();
      q2.push(-B[i]);
    } else if (x1 > x2 && x2 < 0) {
      Ans += x2;
      q2.pop();
      q2.push(-B[i]);
    }
  }
  return tot >= k;
}
inline void getAns(long long mid) {
  while (!q1.empty()) q1.pop();
  while (!q2.empty()) q2.pop();
  long long Ans = 0, tot = 0;
  for (register long long i = 1; i <= n; ++i) {
    q1.push(A[i]);
    long long x1 = q1.top() + B[i] - mid,
              x2 = q2.size() ? q2.top() + B[i] : 1e18;
    if (x1 <= x2 && x1 <= 0) {
      Ans += x1;
      ++tot;
      q1.pop();
      q2.push(-B[i]);
    } else if (x1 > x2 && x2 < 0) {
      Ans += x2;
      q2.pop();
      q2.push(-B[i]);
    }
  }
  cout << Ans + k * mid << "\n";
}
signed main() {
  n = read(), k = read();
  for (register long long i = 1; i <= n; ++i) A[i] = read();
  for (register long long i = 1; i <= n; ++i) B[i] = read();
  long long l = 0, r = 1e18;
  long long Ans = 1e18;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    if (check(mid))
      Ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  getAns(Ans);
  return 0;
}

### Prompt

Your task is to create 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;
inline long long read() {
  long long w = 1, s = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return w * s;
}
long long n, k;
long long A[1001010], B[1001000];
priority_queue<long long, vector<long long>, greater<long long> > q1, q2;
inline bool check(long long mid) {
  while (!q1.empty()) q1.pop();
  while (!q2.empty()) q2.pop();
  long long Ans = 0, tot = 0;
  for (register long long i = 1; i <= n; ++i) {
    q1.push(A[i]);
    long long x1 = q1.top() + B[i] - mid,
              x2 = q2.size() ? q2.top() + B[i] : 1e18;
    if (x1 <= x2 && x1 <= 0) {
      Ans += x1;
      ++tot;
      q1.pop();
      q2.push(-B[i]);
    } else if (x1 > x2 && x2 < 0) {
      Ans += x2;
      q2.pop();
      q2.push(-B[i]);
    }
  }
  return tot >= k;
}
inline void getAns(long long mid) {
  while (!q1.empty()) q1.pop();
  while (!q2.empty()) q2.pop();
  long long Ans = 0, tot = 0;
  for (register long long i = 1; i <= n; ++i) {
    q1.push(A[i]);
    long long x1 = q1.top() + B[i] - mid,
              x2 = q2.size() ? q2.top() + B[i] : 1e18;
    if (x1 <= x2 && x1 <= 0) {
      Ans += x1;
      ++tot;
      q1.pop();
      q2.push(-B[i]);
    } else if (x1 > x2 && x2 < 0) {
      Ans += x2;
      q2.pop();
      q2.push(-B[i]);
    }
  }
  cout << Ans + k * mid << "\n";
}
signed main() {
  n = read(), k = read();
  for (register long long i = 1; i <= n; ++i) A[i] = read();
  for (register long long i = 1; i <= n; ++i) B[i] = read();
  long long l = 0, r = 1e18;
  long long Ans = 1e18;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    if (check(mid))
      Ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  getAns(Ans);
  return 0;
}

```