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>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 200005
#define MOD ll(998244353)
using namespace std;
typedef long long ll;
typedef long double ld;
int main()
{
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n, k;
cin >> n >> k;
ll a[n], b[n];
for (ll i = 0; i < n; i++) cin >> a[i];
for (ll i = 0; i < n; i++) cin >> b[i];
vector <ll> g(n);
for (ll i = 0; i < n; i++) g[i] = a[i] - b[i];
sort(g.begin(), g.end());
ll mid = (n - k) / 2;
ll x = -g[mid];
ll ans = 0;
for (ll i = 0; i < n - k; i++) ans += abs(g[i] + x);
ll sum = 0;
x = -g[mid + k];
for (ll i = k; i < n; i++) sum += abs(g[i] + x);
ans = min(ans, sum);
mid--;
if (mid >= 0 && mid < n)
{
x = -g[mid];
ll sum = 0;
for (ll i = 0; i < n - k; i++) sum += abs(g[i] + x);
ans = min(sum, ans);
}
if (mid + k < n && mid + k >= 0)
{
ll sum = 0;
x = -g[mid + k];
for (ll i = k; i < n; i++) sum += abs(g[i] + x);
ans = min(ans, sum);
}
mid += 2;
if (mid >= 0 && mid < n)
{
x = -g[mid];
ll sum = 0;
for (ll i = 0; i < n - k; i++) sum += abs(g[i] + x);
ans = min(sum, ans);
}
if (mid + k < n && mid + k >= 0)
{
ll sum = 0;
x = -g[mid + k];
for (ll i = k; i < n; i++) sum += abs(g[i] + x);
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... |