#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
ll n,k;
ll dif[100005];
ll suf[100005],pre[100005];
ll x;
bool com(ll a,ll b){
return abs(a-x)>abs(b-x);
}
int presum(int l,int r){
return pre[r]-pre[l-1];
}
int sufsum(int l,int r){
return suf[l]-suf[r+1];
}
int main(){
cin>>n>>k;
for(ll i=1;i<=n;i++){
cin>>dif[i];
}
for(ll i=1;i<=n;i++){
ll x;
cin>>x;
dif[i]=x-dif[i];
}
sort(dif+1,dif+n+1);
for(ll i=1;i<=n;i++){
pre[i]=pre[i-1]+dif[i];
}
for(ll i=n;i>=1;i--){
suf[i]=suf[i+1]+dif[i];
}
ll ans=1e18;
ll l=0,r=n-k+1;
//for(int i=1;i<=n;i++)cout<<dif[i]<<' ';cout<<endl;
for(ll i=1;i<=n;i++){
//cout<<dif[i]*(i-1)-pre[i-1]+suf[i+1]-dif[i]*(n-i)<<endl;
while(abs(dif[l+1]-dif[i])>abs(dif[r]-dif[i])&&r<=n&&l+1<=n){
l++;
r++;
}
//cout<<l<<' '<<r<<endl;
ans=min(ans,dif[i]*(i-1-l)-presum(l+1,i-1)+sufsum(i+1,r-1)-dif[i]*(r-1-i));
/*if(ans>dif[i]*(i-1)-pre[i-1]+suf[i+1]-dif[i]*(n-i)){
ans=dif[i]*(i-1)-pre[i-1]+suf[i+1]-dif[i]*(n-i);
x=dif[i];
}*/
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
79 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
86 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
88 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
2692 KB |
Output is correct |
2 |
Incorrect |
122 ms |
2808 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
76 ms |
2684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
74 ms |
2712 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
95 ms |
2708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
87 ms |
2800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
85 ms |
2664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |