#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e12;
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<long long> b(n);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  auto check = [&](const long long &x) {
    priority_queue<long long> p, q;
    int cnt = 0;
    for (int i = n - 1; i >= 0; i--) {
      q.emplace(x - b[i]);
      long long s = p.empty() ? -inf : p.top();
      long long t = q.top();
      if (s < t) {
        if (a[i] < t) {
          cnt += 1;
          p.emplace(a[i]);
          q.pop();
        }
      } else if (a[i] < s) {
        p.pop();
        p.emplace(a[i]);
      }
    }
    return cnt <= k;
  };
  long long l = -1, r = inf;
  while (r - l > 1) {
    long long mid = (l + r) / 2;
    if (check(mid)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  long long x = l;
  long long ans = 0;
  priority_queue<long long> p, q;
  for (int i = n - 1; i >= 0; i--) {
    q.emplace(x - b[i]);
    long long s = p.empty() ? -inf : p.top();
    long long t = q.top();
    if (s < t) {
      if (a[i] < t) {
        p.emplace(a[i]);
        q.pop();
        ans += a[i] - t;
      }
    } else if (a[i] < s) {
      p.pop();
      p.emplace(a[i]);
      ans += a[i] - s;
    }
  }
  ans += (long long)k * x;
  cout << ans << "\n";
}

### Prompt

Generate 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 long long inf = (long long)1e12;
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<long long> b(n);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  auto check = [&](const long long &x) {
    priority_queue<long long> p, q;
    int cnt = 0;
    for (int i = n - 1; i >= 0; i--) {
      q.emplace(x - b[i]);
      long long s = p.empty() ? -inf : p.top();
      long long t = q.top();
      if (s < t) {
        if (a[i] < t) {
          cnt += 1;
          p.emplace(a[i]);
          q.pop();
        }
      } else if (a[i] < s) {
        p.pop();
        p.emplace(a[i]);
      }
    }
    return cnt <= k;
  };
  long long l = -1, r = inf;
  while (r - l > 1) {
    long long mid = (l + r) / 2;
    if (check(mid)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  long long x = l;
  long long ans = 0;
  priority_queue<long long> p, q;
  for (int i = n - 1; i >= 0; i--) {
    q.emplace(x - b[i]);
    long long s = p.empty() ? -inf : p.top();
    long long t = q.top();
    if (s < t) {
      if (a[i] < t) {
        p.emplace(a[i]);
        q.pop();
        ans += a[i] - t;
      }
    } else if (a[i] < s) {
      p.pop();
      p.emplace(a[i]);
      ans += a[i] - s;
    }
  }
  ans += (long long)k * x;
  cout << ans << "\n";
}

```