#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (int)(1000000007)
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n, k;
cin>>n>>k;
vector<int>A(n);
for(int i=0;i<n;i++){
cin>>A[i];
}
for(int i=0, a;i<n;i++){
cin>>a;
A[i]-=a;
}
sort(all(A));
vector<int>Pre(n);
Pre[0]=A[0];
int ans=1e18;
for(int i=1;i<n;i++)
Pre[i]=Pre[i-1]+A[i];
for(int i=0, r=n-k-1, md=(n-k)/2;r<n;i++, r++, md++){
int val=A[md];
int sum=(md-i+1)*val-(Pre[md]-(i?Pre[i-1]:0))+(r+1?Pre[r]:0)-Pre[md]-val*(r-(md+1)+1);
ans=min(ans, sum);
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
1868 KB |
Output is correct |
2 |
Correct |
21 ms |
1864 KB |
Output is correct |
3 |
Correct |
20 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
1872 KB |
Output is correct |
2 |
Correct |
26 ms |
1860 KB |
Output is correct |
3 |
Correct |
25 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
1892 KB |
Output is correct |
2 |
Correct |
21 ms |
1876 KB |
Output is correct |
3 |
Correct |
25 ms |
1892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
1872 KB |
Output is correct |
2 |
Correct |
19 ms |
3276 KB |
Output is correct |
3 |
Correct |
26 ms |
3268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
1884 KB |
Output is correct |
2 |
Correct |
26 ms |
1864 KB |
Output is correct |
3 |
Correct |
24 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
1884 KB |
Output is correct |
2 |
Correct |
21 ms |
1876 KB |
Output is correct |
3 |
Correct |
28 ms |
3292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
1876 KB |
Output is correct |
2 |
Correct |
22 ms |
3288 KB |
Output is correct |
3 |
Correct |
27 ms |
3276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
1868 KB |
Output is correct |
2 |
Correct |
25 ms |
1876 KB |
Output is correct |
3 |
Correct |
28 ms |
3288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
1864 KB |
Output is correct |
2 |
Correct |
20 ms |
1876 KB |
Output is correct |
3 |
Correct |
25 ms |
1888 KB |
Output is correct |