#include <bits/stdc++.h>
using namespace::std;
long long a[100005],b[100005],s[100005],n,k,i,ans=LLONG_MAX;
vector <int> v;
long long resi(int l,int d,int x){
//cout<<l<<" "<<d<<" "<<x<<endl;
int p=lower_bound(v.begin(),v.end(),x)-v.begin();
p--;
long long res;
res+=(p-l+1)*x;
res-=s[p+1]-s[l];
p=lower_bound(v.begin(),v.end(),x)-v.begin();
res+=s[d+1]-s[p];
res-=x*(d-p+1);
return res;
}
int main(){
std::ios_base::sync_with_stdio(0);
cin>>n>>k;
for(i=0;i<n;i++) cin>>a[i];
for(i=0;i<n;i++) {cin>>b[i]; v.push_back(2*(a[i]-b[i]));}
sort(v.begin(),v.end());
s[0]=0;
for(i=1;i<=n;i++) s[i]=s[i-1]+v[i-1];
for(i=0;i<k+1;i++){
int x,m,d;
d=n-k-1;
if(i>0){
x=(v[i-1]+v[d])/2;
ans=min(ans,resi(i,d,x));
}
if(d<n-1){
x=(v[d+1]+v[i])/2;
ans=min(ans,resi(i,d,x));
}
m=(i+d)/2;
ans=min(ans,resi(i,d,v[m]));
}
cout<<ans/2<<endl;
return 0;
}
Compilation message
simfonija.cpp: In function 'long long int resi(int, int, int)':
simfonija.cpp:13:4: warning: 'res' is used uninitialized in this function [-Wuninitialized]
res+=(p-l+1)*x;
~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
4568 KB |
Output is correct |
2 |
Correct |
36 ms |
4596 KB |
Output is correct |
3 |
Incorrect |
34 ms |
4596 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
4652 KB |
Output is correct |
2 |
Correct |
37 ms |
4724 KB |
Output is correct |
3 |
Correct |
32 ms |
4724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
4552 KB |
Output is correct |
2 |
Correct |
48 ms |
4724 KB |
Output is correct |
3 |
Correct |
46 ms |
4596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
3960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
4596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
4544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
4724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
61 ms |
4696 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
4596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |