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 <queue>
using namespace std;
using ll = long long;
const int maxn = int(1e5) + 10;
const int inf = int(1e8);
const ll linf = 1ll<<60;
int n, k;
int a[maxn], b[maxn];
ll f(int x) {
priority_queue<int> pq;
ll pqs = 0;
for (int i=1; i<=n; ++i) {
int d = abs(a[i]+x-b[i]);
pq.push(d); pqs += d;
if (int(pq.size()) > n-k) {
pqs -= pq.top(); pq.pop();
}
}
return pqs;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i=1; i<=n; ++i) cin >> a[i];
for (int i=1; i<=n; ++i) cin >> b[i];
int l = -inf, r = inf;
while (l+3 < r) {
int mid = l + (r-l)/2;
if (f(mid) < f(mid+1)) r = mid+1;
else l = mid;
}
ll ans = linf;
for (int i=l; i<=r; ++i) ans = min(ans, f(i));
cout << ans << '\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... |