#include <bits/stdc++.h>
using namespace std;
template <typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using max_pq = priority_queue<T>;
const int MAXN = 1 << 19;
int n, k;
long long a[MAXN], b[MAXN];
pair<long long, int> solve(long long x) {
  min_pq<long long> A;
  max_pq<long long> B;
  int i;
  long long sol = 0;
  int cnt = 0;
  for (i = 0; i < n; ++i) {
    A.push(a[i]);
    long long p = 0, q = 0;
    if (!A.empty()) p = A.top() + b[i] - x;
    if (!B.empty()) q = b[i] - B.top();
    if (p < q && p < 0) {
      A.pop();
      B.push(b[i]);
      sol += p;
      ++cnt;
    } else if (q < 0) {
      B.pop();
      B.push(b[i]);
      sol += q;
    }
  }
  return {sol, cnt};
}
int main() {
  int i;
  cin >> n >> k;
  for (i = 0; i < n; ++i) cin >> a[i];
  for (i = 0; i < n; ++i) cin >> b[i];
  long long lo = 0, hi = 1ll << 37, mid, p;
  while (hi - lo > 1) {
    mid = (lo + hi) / 2;
    p = solve(mid).second;
    if (p < k)
      lo = mid;
    else
      hi = mid;
  }
  cout << solve(hi).first + k * hi << endl;
}

### 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;
template <typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using max_pq = priority_queue<T>;
const int MAXN = 1 << 19;
int n, k;
long long a[MAXN], b[MAXN];
pair<long long, int> solve(long long x) {
  min_pq<long long> A;
  max_pq<long long> B;
  int i;
  long long sol = 0;
  int cnt = 0;
  for (i = 0; i < n; ++i) {
    A.push(a[i]);
    long long p = 0, q = 0;
    if (!A.empty()) p = A.top() + b[i] - x;
    if (!B.empty()) q = b[i] - B.top();
    if (p < q && p < 0) {
      A.pop();
      B.push(b[i]);
      sol += p;
      ++cnt;
    } else if (q < 0) {
      B.pop();
      B.push(b[i]);
      sol += q;
    }
  }
  return {sol, cnt};
}
int main() {
  int i;
  cin >> n >> k;
  for (i = 0; i < n; ++i) cin >> a[i];
  for (i = 0; i < n; ++i) cin >> b[i];
  long long lo = 0, hi = 1ll << 37, mid, p;
  while (hi - lo > 1) {
    mid = (lo + hi) / 2;
    p = solve(mid).second;
    if (p < k)
      lo = mid;
    else
      hi = mid;
  }
  cout << solve(hi).first + k * hi << endl;
}

```