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;
typedef long long int ll;
typedef pair<int, int> pii;
const int MAXN = 1e5 + 10, MAX = 2e6 + 10;
int n, k, dif, dsol;
int a[MAXN], b[MAXN];
int s[2 * MAX];
ll cur, sol = 4e18;
vector<int> vd;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin >> n >> k;
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++){
s[b[i] - a[i] + 1 + MAX] += 2;
}
for(int i = 0;i < n; i++){
int p = a[i] - MAX;
cur += b[i] - p + 1;
}
dif = -n;
for(int i = -MAX;i < MAX; i++){
dif += s[i + MAX];
cur += dif;
if(cur < sol){
sol = cur;
dsol = i;
}
}
for(int i = 0;i < n; i++){
a[i] += dsol;
vd.push_back(abs(b[i] - a[i]));
}
sort(vd.begin(), vd.end());
reverse(vd.begin(), vd.end());
for(int i = 0;i < min(k, (int)vd.size()); i++){
sol -= vd[i];
}
cout << sol << "\n";
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... |