#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;
if(N==K){cout<<0;return 0;}
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;
}
}
while(SZ(rgt)&&*rgt.begin()==cutoff){
lft.insert(cutoff);
rgt.erase(rgt.begin());
}
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);
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 |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
7408 KB |
Output is correct |
2 |
Correct |
49 ms |
7408 KB |
Output is correct |
3 |
Correct |
49 ms |
7408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
7412 KB |
Output is correct |
2 |
Correct |
50 ms |
7408 KB |
Output is correct |
3 |
Correct |
51 ms |
7408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
7404 KB |
Output is correct |
2 |
Correct |
47 ms |
7408 KB |
Output is correct |
3 |
Correct |
49 ms |
7404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
49 ms |
4080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
6128 KB |
Output is correct |
2 |
Incorrect |
76 ms |
3440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
2812 KB |
Output is correct |
2 |
Incorrect |
56 ms |
5744 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
4720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
5360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
5928 KB |
Output is correct |
2 |
Correct |
56 ms |
7364 KB |
Output is correct |
3 |
Correct |
56 ms |
6260 KB |
Output is correct |