#include <bits/stdc++.h>
using namespace std;
long long ans;
int n, a[500500], b[500500];
priority_queue<long long, vector<long long>, greater<long long> > qa, qb;
int chk(int lim) {
  int cnt = 0;
  ans = 0;
  for (int i = n; i; i--) {
    qb.push(b[i]);
    long long buf1 = qb.top() + a[i] + lim,
              buf2 = qa.empty() ? 1 : a[i] + qa.top();
    if (buf1 > 0 && buf2 > 0) continue;
    if (buf1 < buf2) {
      qa.push(-a[i]);
      qb.pop();
      cnt++;
      ans += buf1;
    } else {
      qa.pop();
      qa.push(-a[i]);
      ans += buf2;
    }
  }
  while (!qa.empty()) qa.pop();
  while (!qb.empty()) qb.pop();
  return cnt;
}
int k;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  long long l = -2000000000, r = 0, mid;
  while (l < r) {
    if (chk(mid = (l + r) >> 1) <= k)
      r = mid;
    else
      l = mid + 1;
  }
  chk(r);
  printf("%lld", ans - k * r);
  return 0;
}
