This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const maxn = 101010;
int const maxm = 1010101;
ll const inf = 0x3f3f3f3f3f3f3f3f;
ll a[maxn], v[maxn];
ll p[maxn];
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
v[i] = a[i]-x;
}
sort(v+1, v+n+1);
for (int i = 1; i <= n; i++)
p[i] = p[i-1] + v[i];
ll ans = inf;
for (int l = 1; l + n - k - 1 <= n; l++) {
int r = l+n-k-1;
int idx = (l+r)/2;
int a = idx-l;
int b = r-idx+1;
ll soma_a = p[idx-1] - p[l-1];
ll soma_b = p[r] - p[idx-1];
ll sum = - soma_a + a*v[idx] + soma_b - b*v[idx];
sum = abs(sum);
ans = min(ans, sum);
}
cout << ans << "\n";
}
# | 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... |