#include <bits/stdc++.h>
using i64 = long long;
const i64 Inf = 1e10;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, k;
  std::cin >> n >> k;
  std::vector<i64> a(n), b(n);
  for (int i = 0; i < n; ++i) std::cin >> a[i];
  for (int i = 0; i < n; ++i) std::cin >> b[i];
  std::priority_queue<std::pair<i64, int>> hp;
  i64 l = 0, r = Inf;
  while (r - l > 1) {
    i64 mid = (l + r) / 2;
    for (int i = 0; i < n; ++i) {
      hp.emplace(-a[i], 0);
      if (b[i] - mid - hp.top().first < 0) {
        hp.pop();
        hp.emplace(b[i] - mid, 1);
      }
    }
    int cnt = 0;
    while (!hp.empty()) {
      cnt += hp.top().second;
      hp.pop();
    }
    if (cnt <= k)
      l = mid;
    else
      r = mid;
  }
  i64 sum = 0;
  for (int i = 0; i < n; ++i) {
    hp.emplace(-a[i], 0);
    if (b[i] - l - hp.top().first < 0) {
      sum += b[i] - l - hp.top().first;
      hp.pop();
      hp.emplace(b[i] - l, 1);
    }
  }
  while (!hp.empty()) {
    sum += l * hp.top().second;
    hp.pop();
  }
  std::cout << sum << '\n';
  return 0;
}

### 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 i64 = long long;
const i64 Inf = 1e10;
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, k;
  std::cin >> n >> k;
  std::vector<i64> a(n), b(n);
  for (int i = 0; i < n; ++i) std::cin >> a[i];
  for (int i = 0; i < n; ++i) std::cin >> b[i];
  std::priority_queue<std::pair<i64, int>> hp;
  i64 l = 0, r = Inf;
  while (r - l > 1) {
    i64 mid = (l + r) / 2;
    for (int i = 0; i < n; ++i) {
      hp.emplace(-a[i], 0);
      if (b[i] - mid - hp.top().first < 0) {
        hp.pop();
        hp.emplace(b[i] - mid, 1);
      }
    }
    int cnt = 0;
    while (!hp.empty()) {
      cnt += hp.top().second;
      hp.pop();
    }
    if (cnt <= k)
      l = mid;
    else
      r = mid;
  }
  i64 sum = 0;
  for (int i = 0; i < n; ++i) {
    hp.emplace(-a[i], 0);
    if (b[i] - l - hp.top().first < 0) {
      sum += b[i] - l - hp.top().first;
      hp.pop();
      hp.emplace(b[i] - l, 1);
    }
  }
  while (!hp.empty()) {
    sum += l * hp.top().second;
    hp.pop();
  }
  std::cout << sum << '\n';
  return 0;
}

```