#include <bits/stdc++.h>
#define sz(x) ((ll)x.size())
#define MP make_pair
#define PB push_back
#define ft first
#define sd second
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>
using namespace std;
typedef long long ll;
const ll N = 100100;
const ll OO = 1e18;
vector<pll> lf, rt;
vector<pll> qr;
ll n, k, a[N], b[N], sum_lf, sum_rt, extra_lf, extra_rt, pref = 0;
int ptr_lf, ptr_rt;
ll extra(){
return extra_lf + extra_rt + pref * ll(ptr_lf) - pref * ll(ptr_rt);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
for (ll i = 0; i < n; i++)
cin >> a[i];
for (ll j = 0; j < n; j++){
cin >> b[j];
qr.PB(MP(b[j] - a[j], j));
}
ll ans = OO;
sort(all(qr));
int i = 0, j = 0;
while (j < n && qr[i].ft == qr[j].ft){
lf.PB(MP(0, qr[i].sd));
j++;
}
i = j;
if (j == sz(qr)){
cout << 0;
return 0;
}
ptr_rt = min(n - j, k);
ptr_lf = k - ptr_rt;
for (int I = j; I < n; I++){
ll nw = -(a[qr[I].sd] + qr[0].ft) + b[qr[I].sd];
rt.PB(qr[I]);
sum_rt += nw;
if (I + ptr_rt >= n){
extra_rt += nw;
}
}
reverse(all(rt));
ans = min(sum_lf + sum_rt - extra(), ans);
for (; i < n; ){
pref += qr[i].ft - qr[i - 1].ft;
j = i;
while (j < n && qr[i].ft == qr[j].ft){
sum_rt += a[qr[j].sd] + qr[0].ft - b[qr[j].sd];
lf.PB(MP(-pref, qr[j].sd));
sum_lf += -pref;
if (ptr_rt > sz(rt)){
extra_rt += a[qr[j].sd] + qr[0].ft - b[qr[j].sd];
ptr_rt--;
extra_lf += lf[ptr_lf].ft;
ptr_lf++;
}
rt.pop_back();
j++;
}
while (ptr_rt > 0 && lf[ptr_lf].ft + pref >= -(a[rt[ptr_rt - 1].sd] + qr[0].ft) + b[qr[ptr_rt - 1].sd] - pref) {
extra_lf += lf[ptr_lf].ft + pref;
ptr_lf++;
extra_rt -= -(a[rt[ptr_rt - 1].sd] + qr[0].ft) + b[qr[ptr_rt - 1].sd] - pref;
}
ans = min(ans, sum_lf + sum_rt + pref * sz(lf) - pref * sz(rt) - extra());
i = j;
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
8168 KB |
Output is correct |
2 |
Correct |
46 ms |
8184 KB |
Output is correct |
3 |
Correct |
46 ms |
8172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
8180 KB |
Output is correct |
2 |
Correct |
48 ms |
8208 KB |
Output is correct |
3 |
Correct |
46 ms |
8172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
8172 KB |
Output is correct |
2 |
Correct |
48 ms |
8176 KB |
Output is correct |
3 |
Correct |
47 ms |
8172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
44 ms |
11880 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
56 ms |
13876 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
8276 KB |
Output is correct |
2 |
Runtime error |
51 ms |
10512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
57 ms |
13296 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
51 ms |
10472 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
53 ms |
10508 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |