# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
104590 | Hassoony | Simfonija (COCI19_simfonija) | C++17 | 58 ms | 2168 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>
using namespace std;
typedef long long ll;
const ll mod=(1e9+7);
const ll inf=(1ll<<61);
const int MX=1e5+9;
int n,k,a[MX],x;
int main(){
cin>>n>>k;
for(int i=0;i<n;i++)scanf("%d",&a[i]);
for(int i=0;i<n;i++)scanf("%d",&x),a[i]-=x;
sort(a,a+n);
int p1=0,p2=n-1;
ll ans=0;
bool ok=0;
while(k--){
int mid=(p1+p2)/2;
if(abs(a[mid]-a[p1]) > abs(a[p2]-a[mid]))p1++;
else if(abs(a[mid]-a[p1]) < abs(a[p2]-a[mid]))p2--;
else{
if(k>=1){
p1++;p2--;
k--;
}
else ok=1;
}
}
if(!ok){
int x=a[(p1+p2)/2];
for(int i=p1;i<=p2;i++){
a[i]-=x;
ans+=abs(a[i]);
}
cout<<ans<<endl;
}
else{
ll ans1=0;
p1++;
int x=a[(p1+p2)/2];
for(int i=p1;i<=p2;i++){
a[i]-=x;
ans1+=abs(a[i]);
}
p1--;p2--;
x=a[(p1+p2)/2];
for(int i=p1;i<=p2;i++){
a[i]-=x;
ans+=abs(a[i]);
}
cout<<min(ans,ans1)<<endl;
}
}
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... |