#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(int a,int 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);
for(int i=1;i<=k&&i<=n;i++){
ans-=abs(dif[i]-x);
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
2716 KB |
Output is correct |
2 |
Correct |
93 ms |
2672 KB |
Output is correct |
3 |
Correct |
90 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
2652 KB |
Output is correct |
2 |
Correct |
121 ms |
2696 KB |
Output is correct |
3 |
Correct |
100 ms |
2720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
2740 KB |
Output is correct |
2 |
Correct |
100 ms |
2680 KB |
Output is correct |
3 |
Correct |
98 ms |
2612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
2808 KB |
Output is correct |
2 |
Correct |
92 ms |
4084 KB |
Output is correct |
3 |
Incorrect |
97 ms |
4088 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
130 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
2680 KB |
Output is correct |
2 |
Incorrect |
84 ms |
2684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
116 ms |
2804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
107 ms |
2684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
91 ms |
2688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |