#include <bits/stdc++.h>
using namespace std;
const int MAX = 1 << 19;
int a[MAX], b[MAX], n, k;
pair<long long, long long> calc(long long x) {
  long long cnt = 0, val = 0;
  priority_queue<long long> Qa, Qb;
  for (int i = (0); i < (n); i++) {
    Qa.push(-a[i]);
    long long stav = b[i] + x + (-Qa.top());
    long long prom = Qb.empty() ? (long long)(1e18) : b[i] - Qb.top();
    if (stav < prom && stav < 0) {
      cnt++, val += stav;
      Qa.pop();
      Qb.push(b[i]);
    } else if (prom < 0) {
      val += prom;
      Qb.pop();
      Qb.push(b[i]);
    }
  }
  return {cnt, val};
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = (0); i < (n); i++) scanf("%d", &a[i]);
  for (int i = (0); i < (n); i++) scanf("%d", &b[i]);
  long long lo = -1e12, hi = 1e12;
  for (; lo < hi;) {
    long long mid = (lo + hi) / 2;
    if (hi < 0) mid = (lo + hi - 1) / 2;
    if (calc(mid).first > k)
      lo = mid + 1;
    else
      hi = mid;
  }
  printf("%lld\n", calc(lo).second - lo * k);
  return 0;
}
