#include <bits/stdc++.h>
using namespace std;
priority_queue<long long, vector<long long>, greater<long long> > Qa;
priority_queue<long long> Qb;
long long a[500050], b[500050];
int n, m;
const long long INF = 1e13;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%I64d", a + i);
  for (int i = 1; i <= n; i++) scanf("%I64d", b + i);
  long long st = 0, en = INF, ans = 0;
  while (st <= en) {
    long long mid = (st + en) >> 1;
    while (!Qa.empty()) Qa.pop();
    while (!Qb.empty()) Qb.pop();
    long long tmp = 0;
    int 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) {
        tmp += tmp1;
        sz++;
        Qb.push(b[i]);
        Qa.pop();
      } else if (tmp2 < tmp1 && tmp2 < 0) {
        tmp += tmp2;
        Qb.pop();
        Qb.push(b[i]);
      }
    }
    if (sz >= m) {
      ans = tmp + m * mid;
      en = mid - 1;
    } else {
      st = mid + 1;
    }
  }
  cout << ans << endl;
}

### Prompt

Create a solution in cpp for 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;
priority_queue<long long, vector<long long>, greater<long long> > Qa;
priority_queue<long long> Qb;
long long a[500050], b[500050];
int n, m;
const long long INF = 1e13;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%I64d", a + i);
  for (int i = 1; i <= n; i++) scanf("%I64d", b + i);
  long long st = 0, en = INF, ans = 0;
  while (st <= en) {
    long long mid = (st + en) >> 1;
    while (!Qa.empty()) Qa.pop();
    while (!Qb.empty()) Qb.pop();
    long long tmp = 0;
    int 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) {
        tmp += tmp1;
        sz++;
        Qb.push(b[i]);
        Qa.pop();
      } else if (tmp2 < tmp1 && tmp2 < 0) {
        tmp += tmp2;
        Qb.pop();
        Qb.push(b[i]);
      }
    }
    if (sz >= m) {
      ans = tmp + m * mid;
      en = mid - 1;
    } else {
      st = mid + 1;
    }
  }
  cout << ans << endl;
}

```