#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int a[N], b[N];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  for (int i = 1; i <= n; ++i) scanf("%d", b + i);
  int l = 0, r = 2000000000, K;
  long long res = 1e18;
  while (l <= r) {
    K = ((unsigned)l + (unsigned)r) >> 1;
    priority_queue<pair<int, bool>, vector<pair<int, bool>>,
                   greater<pair<int, bool>>>
        q;
    long long now = 0;
    for (int i = 1; i <= n; ++i) {
      q.emplace(a[i], false);
      int aug = b[i] - K + q.top().first;
      if (aug < 0) {
        q.pop();
        now += aug;
        q.emplace(K - b[i], true);
      }
    }
    int cnt = 0;
    while (!q.empty()) cnt += q.top().second, q.pop();
    if (cnt >= k) {
      res = now + 1LL * k * K;
      r = K - 1;
    } else
      l = K + 1;
  }
  printf("%lld\n", res);
  return 0;
}

### Prompt

Your challenge is to write 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;
const int N = 500005;
int a[N], b[N];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  for (int i = 1; i <= n; ++i) scanf("%d", b + i);
  int l = 0, r = 2000000000, K;
  long long res = 1e18;
  while (l <= r) {
    K = ((unsigned)l + (unsigned)r) >> 1;
    priority_queue<pair<int, bool>, vector<pair<int, bool>>,
                   greater<pair<int, bool>>>
        q;
    long long now = 0;
    for (int i = 1; i <= n; ++i) {
      q.emplace(a[i], false);
      int aug = b[i] - K + q.top().first;
      if (aug < 0) {
        q.pop();
        now += aug;
        q.emplace(K - b[i], true);
      }
    }
    int cnt = 0;
    while (!q.empty()) cnt += q.top().second, q.pop();
    if (cnt >= k) {
      res = now + 1LL * k * K;
      r = K - 1;
    } else
      l = K + 1;
  }
  printf("%lld\n", res);
  return 0;
}

```