This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 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 - k + 1) mn = min(mn, calc(l, l + k - 1, pos));
printf("%lld", mn);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |