This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
using namespace std;
#include <iostream>
#include <stack>
#include <algorithm>
#define MAXN 100005
int counter[4000005],A[MAXN],B[MAXN],arr[MAXN],N,K;
int main() {
cin >> N >> K;
long long sum = 0;
for(int i = 0;i < N;++i) cin >> A[i];
for(int i = 0;i < N;++i) cin >> B[i];
for(int i = 0;i < N;++i) {
arr[i] = A[i] - B[i] + 2000001;
counter[arr[i]]++;
sum += arr[i];
}
sort(arr,arr + N);
stack<int> s;
for(int i = 0;i < K;++i) {
s.push(N - i - 1);
sum -= arr[N - i - 1];
}
long long ans = sum;
int inc = 0;
int ptr = 0;
for(int i = 0;i < 4000005;++i) {
inc += counter[i];
sum += inc - (N - K - inc);
while(!s.empty() && (arr[s.top()] - i - 1) < (i + 1 - arr[ptr])) {
sum += arr[s.top()] - arr[ptr++] - 2 * i - 2;
inc--;
s.pop();
}
ans = min(ans,sum);
}
cout << ans << endl;
}
# | 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... |