#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
using ll = long long;
const int maxn = int(1e5) + 10;
const int inf = int(1e8);
const ll linf = 1ll<<60;
int n, k;
int a[maxn], b[maxn];
int d[maxn];
ll pd[maxn];
ll kd[maxn];
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
if (k == n) { cout << "0\n"; return 0; }
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) d[i] = b[i]-a[i];
sort(d+1, d+n+1);
for (int i=1; i<=n; ++i) pd[i] = pd[i-1] + d[i];
for (int i=0; i<=k; ++i) {
if (i+1 <= n)
kd[i] += pd[1+i]-pd[1];
if (n-(k-i) >= 1)
kd[i] += pd[n]-pd[n-(k-i)];
}
int km = max_element(kd, kd+k+1)-kd;
rotate(d+1, d+1+km, d+n+1);
int v = d[1+(n-k)/2];
ll ans = 0;
for (int i=1; i<1+(n-k); ++i) {
ans += abs(v-d[i]);
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
2460 KB |
Output is correct |
2 |
Correct |
29 ms |
2368 KB |
Output is correct |
3 |
Correct |
27 ms |
2344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
2376 KB |
Output is correct |
2 |
Correct |
31 ms |
2376 KB |
Output is correct |
3 |
Correct |
27 ms |
2328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
2416 KB |
Output is correct |
2 |
Correct |
29 ms |
2456 KB |
Output is correct |
3 |
Correct |
29 ms |
2528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
3028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
2564 KB |
Output is correct |
2 |
Incorrect |
33 ms |
3016 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
3128 KB |
Output is correct |
2 |
Incorrect |
29 ms |
2648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
2844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
2784 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
2684 KB |
Output is correct |
2 |
Correct |
29 ms |
2544 KB |
Output is correct |
3 |
Correct |
37 ms |
2748 KB |
Output is correct |