#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
cerr<<vars<<" = ";
string delim="";
(...,(cerr<<delim<<values,delim=", "));
cerr<<"\n";
}
#else
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {}
#endif
#define pb push_back
#define sz(x) (int)(x.size())
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> inline void maxa(T& a,T b){a=max(a,b);}
template<typename T> inline void mina(T& a,T b){a=min(a,b);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mxN=2e5+5; //make sure this is right
const int mod=1e9+7;
int main(){
cin.sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll n,k; cin>>n>>k;
vector<ll> a(n),b(n),A(n),psa(n+1),extra(n+1);
for(auto &i:a) cin>>i;
for(auto &i:b) cin>>i;
for(int i=0;i<n;i++) A[i]=b[i]-a[i];
k=n-k;
sort(A.rbegin(),A.rend());
for(int i=1;i<=n;i++){
psa[i]=psa[i-1]+max(0LL,A[i-1]);
extra[i]=extra[i-1];
if(A[i-1]<0)
extra[i]+=abs(A[i-1]);
}
if(k<=1){
cout<<0<<"\n";
return 0;
}
ll ans=1e18;
for(int i=k;i<=n;i++){
int j=i-(k+1)/2+1;
if(A[j-1]<=0)
break;
ll lhs=psa[j-1]-psa[i-k];
ll rhs=psa[i]-psa[j-1];
ll r=i-j+1;
ll l=k-r;
mina(ans,lhs-l*A[j-1]+r*A[j-1]-rhs+extra[i]);
deb(i,j,ans,lhs,rhs);
}
for(int i=0;i<n;i++) A[i]=-A[i];
sort(A.rbegin(),A.rend());
for(int i=1;i<=n;i++){
psa[i]=psa[i-1]+max(0LL,A[i-1]);
extra[i]=extra[i-1];
if(A[i-1]<0)
extra[i]+=abs(A[i-1]);
}
for(int i=k;i<=n;i++){
int j=i-(k+1)/2+1;
if(A[j-1]<=0)
break;
ll lhs=psa[j-1]-psa[i-k];
ll rhs=psa[i]-psa[j-1];
ll r=i-j+1;
ll l=k-r;
mina(ans,lhs-l*A[j-1]+r*A[j-1]-rhs+extra[i]);
deb(i,ans);
}
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
4172 KB |
Output is correct |
2 |
Correct |
33 ms |
5648 KB |
Output is correct |
3 |
Correct |
27 ms |
5652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
4172 KB |
Output is correct |
2 |
Correct |
32 ms |
5648 KB |
Output is correct |
3 |
Correct |
29 ms |
5672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
4236 KB |
Output is correct |
2 |
Correct |
39 ms |
5640 KB |
Output is correct |
3 |
Correct |
27 ms |
5672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
4172 KB |
Output is correct |
2 |
Correct |
26 ms |
5644 KB |
Output is correct |
3 |
Correct |
29 ms |
5656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
4172 KB |
Output is correct |
2 |
Correct |
40 ms |
5664 KB |
Output is correct |
3 |
Correct |
39 ms |
5656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
4172 KB |
Output is correct |
2 |
Correct |
31 ms |
4232 KB |
Output is correct |
3 |
Correct |
33 ms |
5580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
4172 KB |
Output is correct |
2 |
Correct |
29 ms |
5648 KB |
Output is correct |
3 |
Correct |
34 ms |
5580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
4172 KB |
Output is correct |
2 |
Correct |
34 ms |
5668 KB |
Output is correct |
3 |
Correct |
34 ms |
5652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
4172 KB |
Output is correct |
2 |
Correct |
29 ms |
5580 KB |
Output is correct |
3 |
Correct |
34 ms |
5580 KB |
Output is correct |