#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], pr[n + 1];
pr[n] = 0;
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());
g.pb(1e18);
for (ll i = 0; i < n; i++)
{
pr[i] = 0;
if (i != 0) pr[i] = pr[i - 1];
pr[i] += g[i];
}
ll ans = 1e18, l = 0, r = n - k - 1;
while (r < n)
{
ll mdr = (r - l + 1) / 2;
ll mdl = mdr - 1;
if (mdr >= 0 && mdr < n)
{
ll x = g[mdr];
ll pos = upper_bound(g.begin(), g.end(), x) - g.begin();
ll posr = lower_bound(g.begin(), g.end(), 0) - g.begin();
if (x < 0)
{
ll sum = 0;
if (posr <= r) sum = abs((r - max(posr, l) + 1) * x) + (pr[r] - (max(posr, l) == 0 ? 0 : pr[max(posr, l) - 1]));
if (pos >= l) sum += abs(pr[min(pos - 1, r)] - (l == 0 ? 0 : pr[l - 1]) - x * (min(pos - 1, r) - l + 1));
sum += pr[min(r, posr - 1)] - (max(pos, l) == 0 ? 0 : pr[max(pos, l) - 1]) + abs(x * (min(posr - 1, r) - max(pos, l) + 1));
ans = min(ans, sum);
}
else
{
ll sum = 0;
if (posr - 1 >= l) sum = (posr - l) * x + abs(pr[posr - 1] - (l == 0 ? 0 : pr[l - 1]));
if (pos <= r) sum += pr[r] - (pos == 0 ? 0 : pr[pos - 1]) - (r - pos + 1) * x;
sum += abs(pr[min(r, pos - 1)] - (max(posr, l) == 0 ? 0 : pr[max(posr, l) - 1]) - x * (min(r, pos - 1) - max(posr, l) + 1));
ans = min(ans, sum);
}
}
l++; r++;
}
cout << ans << endl;
}
Compilation message
simfonija.cpp: In function 'int main()':
simfonija.cpp:63:12: warning: unused variable 'mdl' [-Wunused-variable]
ll mdl = mdr - 1;
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
4344 KB |
Output is correct |
2 |
Correct |
36 ms |
4348 KB |
Output is correct |
3 |
Correct |
35 ms |
4336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4344 KB |
Output is correct |
2 |
Correct |
34 ms |
4344 KB |
Output is correct |
3 |
Correct |
35 ms |
4344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4332 KB |
Output is correct |
2 |
Correct |
37 ms |
4344 KB |
Output is correct |
3 |
Correct |
36 ms |
4344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
4224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
40 ms |
4332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
4344 KB |
Output is correct |
2 |
Incorrect |
36 ms |
4328 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
40 ms |
4472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
4344 KB |
Output is correct |
2 |
Incorrect |
44 ms |
4344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
4348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |