#include <bits/stdc++.h>
using namespace std;
long long a[500007], b[500007], n, K;
priority_queue<long long, vector<long long>, greater<long long> > q1, q2;
long long check(long long mid, long long &ans) {
  for (long long i = 1; i <= n; i++) b[i] -= mid;
  while (!q1.empty()) q1.pop();
  while (!q2.empty()) q2.pop();
  ans = 0;
  long long cnt = 0;
  for (long long i = n; i >= 1; i--) {
    q1.push(b[i]);
    if (!q2.empty()) {
      if (a[i] + min(q1.top(), q2.top()) <= 0) {
        if (q1.top() <= q2.top()) {
          ans += a[i] + q1.top();
          q1.pop();
          q2.push(-a[i]);
          cnt++;
        } else
          ans += a[i] + q2.top(), q2.pop(), q2.push(-a[i]);
      }
    } else {
      if (a[i] + q1.top() <= 0) {
        ans += a[i] + q1.top();
        q1.pop();
        cnt++;
        q2.push(-a[i]);
      }
    }
  }
  for (long long i = 1; i <= n; i++) b[i] += mid;
  return cnt;
}
signed main() {
  scanf("%lld%lld", &n, &K);
  for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (long long i = 1; i <= n; i++) scanf("%lld", &b[i]);
  long long l = 0, r = 2e9 + 7;
  long long ans;
  while (l != r) {
    long long mid = l + r >> 1;
    if (check(mid, ans) < K)
      l = mid + 1;
    else
      r = mid;
  }
  check(l, ans);
  ans += 1ll * K * l;
  printf("%lld\n", ans);
  return 0;
}
