#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair
#ifndef ONLINE_JUDGE
# define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
# define LOG(x) ((void)0)
#endif
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double PI=3.1415926535897932384626433832795;
const ll INF = 1LL<<62;
const ll MINF = -1LL<<62;
template<typename T> T getint() {
T val=0;
char c;
bool neg=false;
while((c=gc()) && !(c>='0' && c<='9')) {
neg|=c=='-';
}
do {
val=(val*10)+c-'0';
} while((c=gc()) && (c>='0' && c<='9'));
return val*(neg?-1:1);
}
int main() {
IO;
int n,k;
cin>>n>>k;
if(k==n) cout<<"0\n";
else {
vector<ll> a(n), b(n);
for(int i=0;i<n;++i)
cin>>a[i];
for(int i=0;i<n;++i)
cin>>b[i];
vector<ll> c,sum(n);
for(int i=0;i<n;++i) c.push_back(b[i]-a[i]);
sort(c.begin(), c.end());
sum[0]=c[0];
for(int i=1;i<n;++i) sum[i]+=sum[i-1]+c[i];
//for(auto i:c) cerr<<i<<" ";cerr<<"\n";
int L=-1, R=n-k;
ll ans=1LL<<60;
for(int i=0;i<n;++i) {
while(R<n && abs(c[L+1]-c[i])>abs(c[R]-c[i])) {
L++;
R++;
}
ll cost=0;
if(i-1>=0) {
int len_bal=i-1-L;
if(len_bal>0) {
cost+=len_bal*c[i]-(sum[i-1]-(L>=0?sum[L]:0));
}
}
//cerr<<i<<" "<<L<<" "<<R<<" "<<cost<<"\n";
if(i+1<n) {
int len_jobb=R-1-i;
//cerr<<len_jobb<<"\n";
if(len_jobb>0) {
cost+=sum[R-1]-sum[i]-len_jobb*c[i];
}
}
//cerr<<i<<" "<<L<<" "<<R<<" "<<cost<<"\n";
ans=min(ans, cost);
}
cout<<ans<<"\n";
}
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 |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
3836 KB |
Output is correct |
2 |
Correct |
46 ms |
3876 KB |
Output is correct |
3 |
Correct |
33 ms |
3828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
3828 KB |
Output is correct |
2 |
Correct |
36 ms |
3836 KB |
Output is correct |
3 |
Correct |
32 ms |
3828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
3892 KB |
Output is correct |
2 |
Correct |
34 ms |
3828 KB |
Output is correct |
3 |
Correct |
38 ms |
3908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
3836 KB |
Output is correct |
2 |
Correct |
37 ms |
3956 KB |
Output is correct |
3 |
Correct |
36 ms |
3800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
3836 KB |
Output is correct |
2 |
Correct |
50 ms |
3832 KB |
Output is correct |
3 |
Correct |
44 ms |
3880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
3884 KB |
Output is correct |
2 |
Correct |
30 ms |
3828 KB |
Output is correct |
3 |
Correct |
48 ms |
3920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
3828 KB |
Output is correct |
2 |
Correct |
47 ms |
3960 KB |
Output is correct |
3 |
Correct |
36 ms |
3836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
3828 KB |
Output is correct |
2 |
Correct |
51 ms |
3828 KB |
Output is correct |
3 |
Correct |
36 ms |
3956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
3832 KB |
Output is correct |
2 |
Correct |
32 ms |
3836 KB |
Output is correct |
3 |
Correct |
44 ms |
3832 KB |
Output is correct |