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>
#define ll long long
using namespace std;
const int N=1e5+55;
int a[N];
int b[N];
int x[N];
int n,k;
ll solve(int pos)
{
for(int i=0;i<n;i++)
a[i]=x[i];
int i=0,j=n-1;
int K=k;
while(K--)
{
if(abs(a[i]-a[pos])>abs(a[j]-a[pos]))
a[i]=a[pos],i++;
else
a[j]=a[pos],j--;
}
ll ans=0;
for(;i<=j;i++)
ans+=abs(a[i]-a[pos]);
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>k;
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++)
x[i]=a[i]-b[i];
sort(x,x+n);
cout<<min(solve(n/2),solve(n/2+1))<<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... |