#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 1e5 + 10;
int n, k;
int a[MAXN];
int b[MAXN];
int sim(int x){
vector<int> v;
v.reserve(n);
for(int i=0;i<n;i++) v.push_back( abs(a[i]-b[i] + x));
nth_element(v.begin(),v.begin() + n - k,v.end());
int sm = 0;
for(int i=0;i<n-k;i++) sm += v[i];
return sm;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
#ifdef TPOPPO
freopen("in.txt","r",stdin);
#endif // TPOPPO
cin >> n >> k;
for(int i=0;i<n;i++) cin >> a[i];
for(int i=0;i<n;i++) cin >> b[i];
int l = -2e6;
int r = 2e6;
while(r - l > 5){
//cout<<l<<" "<<r<<endl;
int m1 = l + (r-l)/3;
int m2 = r - (r-l)/3;
//cout<<dx[l]<<" "<<dx[r]<<" => "<<dx[m1]<<": " << sim(dx[m2]) - sim(dx[m1]) <<endl;
if(sim(m1) < sim(m2)){
r = m2;
}else{
l = m1;
}
}
int rs = 1e16;
for(int i=l;i<=r;i++){
//cout<<i<<": "<<sim(i)<<'\n';
rs = min(sim(i),rs);
}
cout<<rs<<'\n';
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 |
47 ms |
2736 KB |
Output is correct |
2 |
Correct |
68 ms |
2728 KB |
Output is correct |
3 |
Correct |
46 ms |
2812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
2680 KB |
Output is correct |
2 |
Correct |
43 ms |
2652 KB |
Output is correct |
3 |
Correct |
47 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
2724 KB |
Output is correct |
2 |
Correct |
50 ms |
2724 KB |
Output is correct |
3 |
Correct |
43 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
98 ms |
2756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
118 ms |
2732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
2748 KB |
Output is correct |
2 |
Correct |
114 ms |
2688 KB |
Output is correct |
3 |
Incorrect |
109 ms |
2764 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
2728 KB |
Output is correct |
2 |
Incorrect |
111 ms |
2688 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
2728 KB |
Output is correct |
2 |
Incorrect |
93 ms |
2680 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
120 ms |
2732 KB |
Output is correct |
2 |
Correct |
82 ms |
2736 KB |
Output is correct |
3 |
Correct |
108 ms |
2688 KB |
Output is correct |