#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 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;
for(ll i=1;i<=n;i++){
//cout<<dif[i]*(i-1)-pre[i-1]+suf[i+1]-dif[i]*(n-i)<<endl;
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];
}
}
sort(dif+1,dif+n+1,com);
ans=0;
for(ll i=k+1;i<=n;i++){
ans+=abs(dif[i]-x);
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 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 |
92 ms |
2652 KB |
Output is correct |
2 |
Correct |
116 ms |
2720 KB |
Output is correct |
3 |
Correct |
107 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
2712 KB |
Output is correct |
2 |
Correct |
86 ms |
2688 KB |
Output is correct |
3 |
Correct |
103 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
2752 KB |
Output is correct |
2 |
Correct |
95 ms |
2680 KB |
Output is correct |
3 |
Correct |
104 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
2672 KB |
Output is correct |
2 |
Correct |
136 ms |
2808 KB |
Output is correct |
3 |
Incorrect |
118 ms |
2680 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
136 ms |
2704 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
2684 KB |
Output is correct |
2 |
Incorrect |
105 ms |
2808 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
120 ms |
2712 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
101 ms |
2684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
95 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |