#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
typedef vector<pi> vpi;
typedef double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll)x.size()
#define f first
#define s second
const ll MAXN=100001;
const ll MAXK=1000001;
const ll INF = 1e13;
const ll MOD = 1e9+7;
vi V;
ll T;
ll A[MAXN];
ll B[MAXN];
ll N,K;
multiset<int> lft;
multiset<int> rgt;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>N>>K;
for(int i=0;i<N;++i){
cin>>A[i];
}
for(int i=0;i<N;++i){
cin>>B[i];
V.pb(B[i]-A[i]);
}
ll res=0;
ll ans=INF;
sort(ALL(V));
int cutoff=V[(N-K)/2];
for(int i=0;i<N-K;++i){
if(V[i]<=cutoff){
lft.insert(V[i]);
res+=cutoff-V[i];
}
else {
rgt.insert(V[i]);
res+=V[i]-cutoff;
}
}
ans=min(ans,res);
for(int i=0;i<K;++i){
rgt.insert(V[i+N-K]);
lft.erase(lft.find(V[i]));
res+=abs(V[i+N-K]-cutoff);
res+=abs(V[i]-cutoff);
// cerr<<"Ers "<<i+N-K<<'\n';
// cerr<<"Ins "<<i<<'\n';
while(SZ(lft) < SZ(rgt)){
ll newct=*rgt.begin();
ll delta=newct-cutoff;
res+=delta*(-SZ(rgt)+SZ(lft));
while(SZ(rgt)&&*rgt.begin()==newct){
lft.insert(newct);
rgt.erase(rgt.begin());
}
}
ans=min(ans,res);
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
7404 KB |
Output is correct |
2 |
Correct |
53 ms |
7540 KB |
Output is correct |
3 |
Correct |
46 ms |
7408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
7404 KB |
Output is correct |
2 |
Correct |
51 ms |
7408 KB |
Output is correct |
3 |
Correct |
45 ms |
7408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
7408 KB |
Output is correct |
2 |
Correct |
51 ms |
7404 KB |
Output is correct |
3 |
Correct |
49 ms |
7408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
44 ms |
4088 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
6132 KB |
Output is correct |
2 |
Incorrect |
58 ms |
4852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
2852 KB |
Output is correct |
2 |
Incorrect |
55 ms |
7280 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
4592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
5388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
6000 KB |
Output is correct |
2 |
Correct |
49 ms |
8816 KB |
Output is correct |
3 |
Correct |
59 ms |
7664 KB |
Output is correct |