#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 = -2e7;
int r = 2e7;
while(r - l > 10){
//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;
int a1 = sim(m1);
int a2 = sim(m2);
while(a1 == a2){
m1--;
a1 = sim(m1);
if(a1 != a2) break;
m2++;
a2 = sim(m2);
}
if(a1 < a2){
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 |
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 |
47 ms |
2744 KB |
Output is correct |
2 |
Correct |
61 ms |
2720 KB |
Output is correct |
3 |
Correct |
50 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
2820 KB |
Output is correct |
2 |
Correct |
81 ms |
2800 KB |
Output is correct |
3 |
Correct |
56 ms |
2792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
2680 KB |
Output is correct |
2 |
Correct |
50 ms |
2680 KB |
Output is correct |
3 |
Correct |
49 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
114 ms |
2724 KB |
Output is correct |
2 |
Incorrect |
125 ms |
2760 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
133 ms |
2748 KB |
Output is correct |
2 |
Incorrect |
113 ms |
2680 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
2728 KB |
Output is correct |
2 |
Incorrect |
124 ms |
2780 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
2736 KB |
Output is correct |
2 |
Correct |
118 ms |
2688 KB |
Output is correct |
3 |
Correct |
120 ms |
2720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1053 ms |
2680 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
2724 KB |
Output is correct |
2 |
Correct |
148 ms |
2744 KB |
Output is correct |
3 |
Correct |
119 ms |
2748 KB |
Output is correct |