// iee
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define fi first
#define se second
using ll = long long;
using ull = unsigned long long;
using namespace std;
void work(int);
template <class T> void read(T &x) {
x = 0;
int f = 1, ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch - '0');
ch = getchar();
}
x *= f;
}
int main() {
int TT = 1; // cin >> TT;
rep(CAS, 1, TT)
work(CAS);
return 0;
}
const int N = 1e5 + 5;
int n, k, a[N], b[N], d[N];
ll s[N];
ll calc(int l, int r, int pos) {
if (pos < l) {
ll ret = (ll) pos * d[pos] - s[pos];
ret += (s[l - 1] - s[pos]) - (ll) d[pos] * (l - 1 - pos);
ret += (s[n] - s[r]) - (ll) d[pos] * (n - r);
return ret;
} else {
pos = r + (pos - (l - 1));
ll ret = (ll) (l - 1) * d[pos] - s[l - 1];
ret += (ll) (pos - r) * d[pos] - (s[pos] - s[r]);
ret += (s[n] - s[pos]) - (ll) (n - pos) * d[pos];
return ret;
}
}
void work(int CASE) {
read(n), read(k);
int pos = (n - k + 1) / 2;
rep(i, 1, n) read(a[i]);
rep(i, 1, n) read(b[i]), d[i] = a[i] - b[i];
sort(d + 1, d + n + 1);
rep(i, 1, n) s[i] = s[i - 1] + d[i];
ll mn = 1e18;
rep(l, 1, n) mn = min(mn, calc(l, l + k - 1, pos));
printf("%lld", mn);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
312 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
3680 KB |
Output is correct |
2 |
Correct |
13 ms |
3584 KB |
Output is correct |
3 |
Correct |
12 ms |
3656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
3668 KB |
Output is correct |
2 |
Correct |
15 ms |
3668 KB |
Output is correct |
3 |
Correct |
12 ms |
3680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
3548 KB |
Output is correct |
2 |
Correct |
14 ms |
3668 KB |
Output is correct |
3 |
Correct |
12 ms |
3668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
3028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
3576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
3540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
3676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
3660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
3656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |