# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
554752 | A_D | Simfonija (COCI19_simfonija) | C++14 | 29 ms | 3300 KiB |
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>
#define int long long
#define ii pair<int,int>
#define F first
#define S second
using namespace std;
const int N=1e5+100;
int a[N];
int pre[N];
void solve()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
int b;
cin>>b;
a[i]-=b;
}
sort(a+1,a+n+1);
for(int i=2;i<=n;i++){
a[i]-=a[1];
}
a[1]=0;
for(int i=1;i<=n;i++){
pre[i]=pre[i-1]+a[i];
}
int l=k+1,r=n;
int ans=1e18;
/*
for(int i=1;i<=n;i++){
cout<<a[i]<<" ";
}
cout<<"\n";
*/
while(l>=1){
int mid=(l+r)/2;
int v=(a[mid]*(mid-l+1)-(pre[mid]-pre[l-1]))+((pre[r]-pre[mid])-(a[mid]*(r-mid)));
ans=min(ans,v);
// cout<<l<<" "<<r<<" "<<v<<"\n";
l--;
r--;
}
cout<<ans<<"\n";
}
main()
{
///*
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//*/
int t=1;
// cin>>t;
while(t--){
solve();
}
}
/*
7
7 2 5 4 7 4 2
*/
Compilation message (stderr)
# | 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... |