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;
const int maxn = 100005;
typedef long long ll;
ll a[maxn], b[maxn], d[maxn], pf[maxn];
int n, k;
ll get_on(int p1, int p2) {
if(p2 < p1) return 0LL;
return pf[p2] - (p1 == 0 ? 0LL : pf[p1-1]);
}
int main()
{
cin>>n>>k;
ll sol = 0LL;
for(int i=0;i<n;i++) {
cin>>a[i];
}
for(int i=0;i<n;i++) {
cin>>b[i];
}
for(int i=0;i<n;i++) {
d[i] = a[i] - b[i];
sol += abs(d[i]);
}
sort(d,d+n);
for(int i=0;i<n;i++) {
pf[i] = (i==0 ? d[i] : pf[i-1] + d[i]);
}
for(int i=0;i<=k;i++) {
ll med = (i + i+n-k-1) / 2;
//cout<<med<<endl;
ll elol = (med-i) * d[med] - get_on(i,med-1);
//cout<<(med-i) * d[med] <<" "<< get_on(i,med-1) <<endl;
ll utan = get_on(med+1, i+n-k-1) - (i+n-k-1-med) * d[med];
//cout<<get_on(med+1, i+n-k-1) <<" "<< (i+n-k-1-med) * d[med]<<endl;
//cout<<elol<<" "<<utan<<endl;
sol = min(sol,elol+utan);
}
cout<<sol<<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... |