This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
}
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |