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;
#define fi first
#define se second
using ll = long long;
const int N = 1e5+5;
//--------------------------------------
int n, k, a[N], b[N];
ll vals[N], s[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) vals[i] = b[i] - a[i];
sort(vals+1, vals+n+1);
for (int i = 1; i <= n; i++) s[i] = s[i-1] + vals[i];
ll ans = 9e18;
for (int L = 0; L <= k; L++) {
int R = k-L, p1 = 1+L, p2 = n-R;
int mid = (p1+p2) / 2;
ll s1 = vals[mid] * (mid-p1+1) - (s[mid] - s[p1-1]);
ll s2 = (s[p2] - s[mid]) - vals[mid] * (p2-mid);
ans = min(ans, s1+s2);
}
cout << ans;
}
# | 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... |