#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
long long n, k, a[N], b[N], ans;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  long long l = 0, r = 2e9;
  while (l < r) {
    priority_queue<pair<long long, long long>,
                   vector<pair<long long, long long> >,
                   greater<pair<long long, long long> > >
        q;
    long long mid = l + r >> 1, num = 0, sum = 0;
    for (int i = 1; i <= n; i++) {
      q.push({a[i] - mid, 0});
      pair<long long, long long> t = q.top();
      if (t.first + b[i] <= 0)
        num += !t.second, sum += t.first + b[i], q.pop(), q.push({-b[i], 1});
    }
    if (num >= k)
      r = mid, ans = sum + k * mid;
    else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Please formulate 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 = 5e5 + 5;
long long n, k, a[N], b[N], ans;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  long long l = 0, r = 2e9;
  while (l < r) {
    priority_queue<pair<long long, long long>,
                   vector<pair<long long, long long> >,
                   greater<pair<long long, long long> > >
        q;
    long long mid = l + r >> 1, num = 0, sum = 0;
    for (int i = 1; i <= n; i++) {
      q.push({a[i] - mid, 0});
      pair<long long, long long> t = q.top();
      if (t.first + b[i] <= 0)
        num += !t.second, sum += t.first + b[i], q.pop(), q.push({-b[i], 1});
    }
    if (num >= k)
      r = mid, ans = sum + k * mid;
    else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}

```