#include <bits/stdc++.h>   using namespace std;   long long a[2210], b[2210];   long long inf = 1e13;   int main() {     memset(a, 0, sizeof(a));     memset(b, 0, sizeof(b));     int n = 0, k = 0;     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) {       scanf( %I64d , &a[i]);     }     for (int i = 1; i <= n; ++i) {       scanf( %I64d , &b[i]);     }     long long l = 0, r = inf, res = 0;     while (l <= r) {       long long mid = (l + r) >> 1;       priority_queue<long long, vector<long long>, greater<long long>> qa;       priority_queue<long long> qb;       long long sz = 0, cur = 0;       for (int i = 1; i <= n; ++i) {         qa.push(a[i]);         long long tmp1 = qa.top() + b[i] - mid;         long long tmp2 = qb.empty() ? inf : (b[i] - qb.top());         if (tmp1 <= tmp2 && tmp1 <= 0) {           cur += tmp1;           ++sz;           qb.push(b[i]);           qa.pop();         } else if (tmp2 < tmp1 && tmp2 < 0) {           cur += tmp2;           qb.pop();           qb.push(b[i]);         }       }       if (sz >= k) {         res = cur + (long long)k * mid;         r = mid - 1;       } else {         l = mid + 1;       }     }     printf( %I64d , res);     return 0;   }   