# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
100337 | pzdba | Simfonija (COCI19_simfonija) | C++14 | 60 ms | 3832 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;
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)
# | 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... |