Submission #101731

#TimeUsernameProblemLanguageResultExecution timeMemory
101731mayhoubsalehSimfonija (COCI19_simfonija)C++14
44 / 110
136 ms2808 KiB
#include <bits/stdc++.h> #define ll long long #define pb push_back using namespace std; ll n,k; ll dif[100005]; ll suf[100005],pre[100005]; ll x; bool com(ll a,ll b){ return abs(a-x)>abs(b-x); } int main(){ cin>>n>>k; for(ll i=1;i<=n;i++){ cin>>dif[i]; } for(ll i=1;i<=n;i++){ ll x; cin>>x; dif[i]=x-dif[i]; } sort(dif+1,dif+n+1); for(ll i=1;i<=n;i++){ pre[i]=pre[i-1]+dif[i]; } for(ll i=n;i>=1;i--){ suf[i]=suf[i+1]+dif[i]; } ll ans=1e18; for(ll i=1;i<=n;i++){ //cout<<dif[i]*(i-1)-pre[i-1]+suf[i+1]-dif[i]*(n-i)<<endl; if(ans>dif[i]*(i-1)-pre[i-1]+suf[i+1]-dif[i]*(n-i)){ ans=dif[i]*(i-1)-pre[i-1]+suf[i+1]-dif[i]*(n-i); x=dif[i]; } } sort(dif+1,dif+n+1,com); ans=0; for(ll i=k+1;i<=n;i++){ ans+=abs(dif[i]-x); } cout<<ans<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...