#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007LL;
long long base = 37;
long long large = 1000000000000000000LL;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n, 0), b(n, 0);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      pq;
  long long low = 0LL, high = 2000000000LL;
  long long mid;
  long long ans;
  long long cost;
  while (low <= high) {
    mid = (low + high) / 2;
    while (!pq.empty()) pq.pop();
    long long tc = 0LL;
    for (int i = 0; i < n; i++) {
      pq.push(pair<long long, int>(a[i], 0));
      long long lt = b[i] - mid;
      if (lt + pq.top().first <= 0) {
        tc += lt + pq.top().first;
        pq.pop();
        pq.push(pair<long long, int>(-lt, 1));
      }
    }
    int s = 0;
    while (!pq.empty()) {
      s += pq.top().second;
      pq.pop();
    }
    if (s >= k) {
      ans = mid;
      high = mid - 1;
      cost = tc + mid * s;
    } else {
      low = mid + 1;
    }
  }
  cout << cost << endl;
  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;
long long mod = 1000000007LL;
long long base = 37;
long long large = 1000000000000000000LL;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n, 0), b(n, 0);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      pq;
  long long low = 0LL, high = 2000000000LL;
  long long mid;
  long long ans;
  long long cost;
  while (low <= high) {
    mid = (low + high) / 2;
    while (!pq.empty()) pq.pop();
    long long tc = 0LL;
    for (int i = 0; i < n; i++) {
      pq.push(pair<long long, int>(a[i], 0));
      long long lt = b[i] - mid;
      if (lt + pq.top().first <= 0) {
        tc += lt + pq.top().first;
        pq.pop();
        pq.push(pair<long long, int>(-lt, 1));
      }
    }
    int s = 0;
    while (!pq.empty()) {
      s += pq.top().second;
      pq.pop();
    }
    if (s >= k) {
      ans = mid;
      high = mid - 1;
      cost = tc + mid * s;
    } else {
      low = mid + 1;
    }
  }
  cout << cost << endl;
  return 0;
}

```