#include <bits/stdc++.h>
using namespace std;
const int N = 500050;
const long long inf = 1e13;
int n, m;
long long a[N], b[N];
priority_queue<long long, vector<long long>, greater<long long> > Q1;
priority_queue<long long> Q2;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
  long long L = 0, R = inf, ans = 0;
  while (L <= R) {
    long long mid = (L + R) >> 1, res = 0;
    int cnt = 0;
    while (!Q1.empty()) Q1.pop();
    while (!Q2.empty()) Q2.pop();
    for (int i = 1; i <= n; i++) {
      Q1.push(a[i]);
      long long t1 = Q1.top() + b[i] - mid,
                t2 = Q2.empty() ? inf : b[i] - Q2.top();
      if (t1 <= t2 && t1 <= 0) {
        res += t1;
        cnt++;
        Q1.pop();
        Q2.push(b[i]);
      } else if (t2 < t1 && t2 < 0) {
        res += t2;
        Q2.pop();
        Q2.push(b[i]);
      }
    }
    if (cnt >= m) {
      ans = res + m * mid;
      R = mid - 1;
    } else
      L = mid + 1;
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Please provide a cpp coded solution 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;
const int N = 500050;
const long long inf = 1e13;
int n, m;
long long a[N], b[N];
priority_queue<long long, vector<long long>, greater<long long> > Q1;
priority_queue<long long> Q2;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
  long long L = 0, R = inf, ans = 0;
  while (L <= R) {
    long long mid = (L + R) >> 1, res = 0;
    int cnt = 0;
    while (!Q1.empty()) Q1.pop();
    while (!Q2.empty()) Q2.pop();
    for (int i = 1; i <= n; i++) {
      Q1.push(a[i]);
      long long t1 = Q1.top() + b[i] - mid,
                t2 = Q2.empty() ? inf : b[i] - Q2.top();
      if (t1 <= t2 && t1 <= 0) {
        res += t1;
        cnt++;
        Q1.pop();
        Q2.push(b[i]);
      } else if (t2 < t1 && t2 < 0) {
        res += t2;
        Q2.pop();
        Q2.push(b[i]);
      }
    }
    if (cnt >= m) {
      ans = res + m * mid;
      R = mid - 1;
    } else
      L = mid + 1;
  }
  cout << ans << endl;
  return 0;
}

```