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<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 |
---|
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... |