#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
using ll = long long;
const int N = 1e5+5;
//--------------------------------------
int n, k, a[N], b[N];
ll vals[N], s[N];
int main() {
ios::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];
for (int i = 1; i <= n; i++) vals[i] = b[i] - a[i];
sort(vals+1, vals+n+1);
for (int i = 1; i <= n; i++) s[i] = s[i-1] + vals[i];
ll ans = 9e18;
for (int L = 0; L <= k; L++) {
int R = k-L, p1 = 1+L, p2 = n-R;
int mid = (p1+p2) / 2;
ll s1 = vals[mid] * (mid-p1+1) - (s[mid] - s[p1-1]);
ll s2 = (s[p2] - s[mid]) - vals[mid] * (p2-mid);
ans = min(ans, s1+s2);
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4176 KB |
Output is correct |
2 |
Correct |
19 ms |
4024 KB |
Output is correct |
3 |
Correct |
17 ms |
4184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
4188 KB |
Output is correct |
2 |
Correct |
18 ms |
4184 KB |
Output is correct |
3 |
Correct |
17 ms |
4176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4184 KB |
Output is correct |
2 |
Correct |
19 ms |
4180 KB |
Output is correct |
3 |
Correct |
16 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
3416 KB |
Output is correct |
2 |
Correct |
16 ms |
4104 KB |
Output is correct |
3 |
Correct |
17 ms |
4136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
4188 KB |
Output is correct |
2 |
Correct |
21 ms |
4156 KB |
Output is correct |
3 |
Correct |
21 ms |
4016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
4024 KB |
Output is correct |
2 |
Correct |
17 ms |
4188 KB |
Output is correct |
3 |
Correct |
20 ms |
4056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
4188 KB |
Output is correct |
2 |
Correct |
20 ms |
4180 KB |
Output is correct |
3 |
Correct |
20 ms |
4188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4384 KB |
Output is correct |
2 |
Correct |
20 ms |
4188 KB |
Output is correct |
3 |
Correct |
20 ms |
4052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4188 KB |
Output is correct |
2 |
Correct |
17 ms |
4152 KB |
Output is correct |
3 |
Correct |
20 ms |
4052 KB |
Output is correct |