Submission #100337

#TimeUsernameProblemLanguageResultExecution timeMemory
100337pzdbaSimfonija (COCI19_simfonija)C++14
110 / 110
60 ms3832 KiB
#include <bits/stdc++.h> using namespace std; int a[100001], b[100001], c[100001]; long long sum[100001]; int main(){ int n, k; scanf("%d%d", &n, &k); for(int i=1;i<=n;i++) scanf("%d", &a[i]); for(int i=1;i<=n;i++) scanf("%d", &b[i]); for(int i=1;i<=n;i++) c[i] = a[i]-b[i]; sort(c+1, c+1+n); if(k == n){ printf("%d\n", 0); return 0; } for(int i=1;i<=n;i++) sum[i] = sum[i-1] + c[i]; int l = 1, r = n-k, p = 1; long long ans = 1e18; for(int x=1000000;x>=-1000000;x--){ while(r < n && abs(c[l]+x) > abs(c[r+1]+x)) l++, r++; p = max(p, l); while(p <= r && c[p]+x < 0) p++; long long add1 = (sum[r] - sum[p-1]) + (r-p+1)*1ll*x; long long add2 = abs((sum[p-1] - sum[l-1]) + ((p-1)-l+1)*1ll*x); ans = min(ans, add1+add2); } printf("%lld\n", ans); }

Compilation message (stderr)

simfonija.cpp: In function 'int main()':
simfonija.cpp:7:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~
simfonija.cpp:8:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1;i<=n;i++) scanf("%d", &a[i]);
                           ~~~~~^~~~~~~~~~~~~
simfonija.cpp:9:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1;i<=n;i++) scanf("%d", &b[i]);
                           ~~~~~^~~~~~~~~~~~~
#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...