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 <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];
int 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=0; i<=k; ++i) {
if (i < k)
kd[i] += d[1+i]-d[1];
if (0 < i)
kd[i] += d[k]-d[k-i];
}
int km = min_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 |
---|
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... |