#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 1;
const long long INF = 1e18;
int n, k, a[N], b[N];
int cnt[N];
long long dp[N];
bool minimize(long long &x, const long long &y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
void calc(int cost) {
  priority_queue<int, vector<int>, greater<int> > pqA;
  priority_queue<int> pqB;
  for (int i = 1; i <= n; i++) {
    pqA.push(a[i]);
    int typ1 = pqA.top() + b[i] - cost;
    int typ2 = (pqB.empty() ? 0 : b[i] - pqB.top());
    if (typ1 < 0 && typ1 <= typ2) {
      dp[i] = dp[i - 1] + typ1;
      cnt[i] = cnt[i - 1] + 1;
      pqA.pop();
      pqB.push(b[i]);
    } else if (typ2 < 0) {
      dp[i] = dp[i - 1] + typ2;
      cnt[i] = cnt[i - 1];
      pqB.pop();
      pqB.push(b[i]);
    } else {
      dp[i] = dp[i - 1];
      cnt[i] = cnt[i - 1];
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  int l = 0, r = 2e9, best = -1;
  while (l <= r) {
    int mid = ((long long)l + r) >> 1;
    calc(mid);
    if (cnt[n] >= k) {
      best = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  assert(best > -1);
  calc(best);
  cout << dp[n] + (long long)(2 * cnt[n] - k) * best;
  return 0;
}

### Prompt

Please create a solution in cpp 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 + 1;
const long long INF = 1e18;
int n, k, a[N], b[N];
int cnt[N];
long long dp[N];
bool minimize(long long &x, const long long &y) {
  if (x > y) {
    x = y;
    return 1;
  }
  return 0;
}
void calc(int cost) {
  priority_queue<int, vector<int>, greater<int> > pqA;
  priority_queue<int> pqB;
  for (int i = 1; i <= n; i++) {
    pqA.push(a[i]);
    int typ1 = pqA.top() + b[i] - cost;
    int typ2 = (pqB.empty() ? 0 : b[i] - pqB.top());
    if (typ1 < 0 && typ1 <= typ2) {
      dp[i] = dp[i - 1] + typ1;
      cnt[i] = cnt[i - 1] + 1;
      pqA.pop();
      pqB.push(b[i]);
    } else if (typ2 < 0) {
      dp[i] = dp[i - 1] + typ2;
      cnt[i] = cnt[i - 1];
      pqB.pop();
      pqB.push(b[i]);
    } else {
      dp[i] = dp[i - 1];
      cnt[i] = cnt[i - 1];
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  int l = 0, r = 2e9, best = -1;
  while (l <= r) {
    int mid = ((long long)l + r) >> 1;
    calc(mid);
    if (cnt[n] >= k) {
      best = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  assert(best > -1);
  calc(best);
  cout << dp[n] + (long long)(2 * cnt[n] - k) * best;
  return 0;
}

```