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 <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
int n, k;
ll a[100005], b[100005], sol, sad, lf, rt, sum;
vector <ll> d;
const ll inf = (ll)1e15;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 0 ; i < n ; ++i){
cin >> a[i];
}
for(int i = 0 ; i < n ; ++i){
cin >> b[i];
}
if(k == n){
cout << 0 << endl;
return 0;
}
for(int i = 0 ; i < n ; ++i){
d.push_back(a[i] - b[i]);
sum += abs(d[i]);
}
sort(d.begin(), d.end());
ll c = n - k, brl = 0, brr = 0;
for(int i = 0 ; i < c / 2 ; ++i){
lf += d[i];
brl++;
sad += abs(d[i]);
}
for(int i = c / 2 ; i < c ; ++i){
rt += d[i];
brr++;
sad += abs(d[i]);
}
sol = sum;
for(int i = 0 ; i <= k ; ++i){
ll med = d[i + c / 2];
ll ovo = brl * med - lf + rt - brr * med;
sol = min(sol, ovo);
lf -= d[i];
lf += med;
rt -= med;
rt += d[i + c];
sad -= abs(d[i]);
sad += abs(d[i + c]);
}
cout << sol << endl;
return 0;
}
# | 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... |