#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
/*
refs:
edi
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
void solve(int test_case)
{
ll n,k; cin >> n >> k;
vector<ll> a(n+5), b(n+5);
rep1(i,n) cin >> a[i];
rep1(i,n) cin >> b[i];
vector<ll> p(n+5);
rep1(i,n) p[i] = p[i-1]+a[i];
multiset<ll> ms1,ms2;
ll curr_sum = 0;
ll lx = 1, rx = 0;
auto transfer = [&](){
while(!ms2.empty() and sz(ms1) < k){
curr_sum += *ms2.rbegin();
ms1.insert(*ms2.rbegin());
ms2.erase(--ms2.end());
}
while(sz(ms1) > k){
ll x = *ms1.begin();
curr_sum -= x;
ms1.erase(ms1.begin());
ms2.insert(x);
}
if(ms2.empty()) return;
while(true){
ll mn1 = *ms1.begin(), mx2 = *ms2.rbegin();
if(mn1 >= mx2) break;
ms1.erase(ms1.find(mn1));
ms2.erase(ms2.find(mx2));
ms1.insert(mx2);
ms2.insert(mn1);
curr_sum += mx2-mn1;
}
};
auto ins = [&](ll i){
ms1.insert(b[i]);
curr_sum += b[i];
transfer();
};
auto del = [&](ll i){
if(ms1.find(b[i]) != ms1.end()){
ms1.erase(ms1.find(b[i]));
curr_sum -= b[i];
}
else{
ms2.erase(ms2.find(b[i]));
}
transfer();
};
auto f = [&](ll l, ll r){
if(r-l+1 < k) return -inf2;
// expand
while(rx < r){
rx++;
ins(rx);
}
while(lx > l){
lx--;
ins(lx);
}
// contract
while(rx > r){
del(rx);
rx--;
}
while(lx < l){
del(lx);
lx++;
}
ll sum = -(p[r]-p[l-1]);
sum += curr_sum;
return sum;
};
ll ans = -inf2;
auto go = [&](ll l, ll r, ll optl, ll optr, auto &&go) -> void{
if(l > r) return;
ll mid = (l+r) >> 1;
ll best = -inf2, optm = -1;
for(int i = optl; i <= optr; ++i){
ll cost = f(mid,i);
if(cost >= best){
best = cost;
optm = i;
}
}
amax(ans,best);
go(l,mid-1,optl,optm,go);
go(mid+1,r,optm,optr,go);
};
go(1,n-k+1,1,n,go);
cout << ans << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
600 KB |
Partially correct |
2 |
Partially correct |
0 ms |
344 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
348 KB |
Partially correct |
2 |
Partially correct |
0 ms |
344 KB |
Partially correct |
3 |
Partially correct |
0 ms |
348 KB |
Partially correct |
4 |
Partially correct |
0 ms |
468 KB |
Partially correct |
5 |
Partially correct |
1 ms |
348 KB |
Partially correct |
6 |
Partially correct |
1 ms |
348 KB |
Partially correct |
7 |
Partially correct |
1 ms |
348 KB |
Partially correct |
8 |
Partially correct |
0 ms |
348 KB |
Partially correct |
9 |
Partially correct |
0 ms |
456 KB |
Partially correct |
10 |
Partially correct |
1 ms |
348 KB |
Partially correct |
11 |
Partially correct |
0 ms |
348 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
348 KB |
Partially correct |
2 |
Partially correct |
0 ms |
344 KB |
Partially correct |
3 |
Partially correct |
0 ms |
348 KB |
Partially correct |
4 |
Partially correct |
0 ms |
468 KB |
Partially correct |
5 |
Partially correct |
1 ms |
348 KB |
Partially correct |
6 |
Partially correct |
1 ms |
348 KB |
Partially correct |
7 |
Partially correct |
1 ms |
348 KB |
Partially correct |
8 |
Partially correct |
0 ms |
348 KB |
Partially correct |
9 |
Partially correct |
0 ms |
456 KB |
Partially correct |
10 |
Partially correct |
1 ms |
348 KB |
Partially correct |
11 |
Partially correct |
0 ms |
348 KB |
Partially correct |
12 |
Partially correct |
0 ms |
348 KB |
Partially correct |
13 |
Partially correct |
0 ms |
348 KB |
Partially correct |
14 |
Partially correct |
1 ms |
348 KB |
Partially correct |
15 |
Partially correct |
1 ms |
344 KB |
Partially correct |
16 |
Partially correct |
1 ms |
348 KB |
Partially correct |
17 |
Partially correct |
0 ms |
348 KB |
Partially correct |
18 |
Partially correct |
0 ms |
348 KB |
Partially correct |
19 |
Partially correct |
0 ms |
348 KB |
Partially correct |
20 |
Partially correct |
0 ms |
348 KB |
Partially correct |
21 |
Partially correct |
1 ms |
348 KB |
Partially correct |
22 |
Partially correct |
0 ms |
348 KB |
Partially correct |
23 |
Partially correct |
2 ms |
860 KB |
Partially correct |
24 |
Partially correct |
13 ms |
868 KB |
Partially correct |
25 |
Partially correct |
23 ms |
900 KB |
Partially correct |
26 |
Partially correct |
21 ms |
1112 KB |
Partially correct |
27 |
Partially correct |
13 ms |
892 KB |
Partially correct |
28 |
Partially correct |
12 ms |
604 KB |
Partially correct |
29 |
Partially correct |
16 ms |
940 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
348 KB |
Partially correct |
2 |
Partially correct |
625 ms |
15776 KB |
Partially correct |
3 |
Partially correct |
1022 ms |
18004 KB |
Partially correct |
4 |
Partially correct |
855 ms |
19540 KB |
Partially correct |
5 |
Partially correct |
895 ms |
17956 KB |
Partially correct |
6 |
Partially correct |
908 ms |
17756 KB |
Partially correct |
7 |
Partially correct |
1813 ms |
20052 KB |
Partially correct |
8 |
Partially correct |
1027 ms |
18112 KB |
Partially correct |
9 |
Partially correct |
913 ms |
16464 KB |
Partially correct |
10 |
Partially correct |
927 ms |
16976 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
348 KB |
Partially correct |
2 |
Partially correct |
625 ms |
15776 KB |
Partially correct |
3 |
Partially correct |
1022 ms |
18004 KB |
Partially correct |
4 |
Partially correct |
855 ms |
19540 KB |
Partially correct |
5 |
Partially correct |
895 ms |
17956 KB |
Partially correct |
6 |
Partially correct |
908 ms |
17756 KB |
Partially correct |
7 |
Partially correct |
1813 ms |
20052 KB |
Partially correct |
8 |
Partially correct |
1027 ms |
18112 KB |
Partially correct |
9 |
Partially correct |
913 ms |
16464 KB |
Partially correct |
10 |
Partially correct |
927 ms |
16976 KB |
Partially correct |
11 |
Partially correct |
0 ms |
344 KB |
Partially correct |
12 |
Partially correct |
602 ms |
15956 KB |
Partially correct |
13 |
Partially correct |
1016 ms |
18260 KB |
Partially correct |
14 |
Partially correct |
861 ms |
19660 KB |
Partially correct |
15 |
Partially correct |
876 ms |
18004 KB |
Partially correct |
16 |
Partially correct |
927 ms |
17748 KB |
Partially correct |
17 |
Partially correct |
1885 ms |
20048 KB |
Partially correct |
18 |
Partially correct |
1091 ms |
18164 KB |
Partially correct |
19 |
Partially correct |
925 ms |
16592 KB |
Partially correct |
20 |
Partially correct |
917 ms |
16980 KB |
Partially correct |
21 |
Partially correct |
0 ms |
344 KB |
Partially correct |
22 |
Partially correct |
0 ms |
348 KB |
Partially correct |
23 |
Partially correct |
1 ms |
348 KB |
Partially correct |
24 |
Partially correct |
1 ms |
348 KB |
Partially correct |
25 |
Partially correct |
1 ms |
348 KB |
Partially correct |
26 |
Partially correct |
0 ms |
348 KB |
Partially correct |
27 |
Partially correct |
1 ms |
600 KB |
Partially correct |
28 |
Partially correct |
1 ms |
348 KB |
Partially correct |
29 |
Partially correct |
1 ms |
348 KB |
Partially correct |
30 |
Partially correct |
1 ms |
348 KB |
Partially correct |
31 |
Partially correct |
1411 ms |
18060 KB |
Partially correct |
32 |
Partially correct |
884 ms |
18004 KB |
Partially correct |
33 |
Partially correct |
1558 ms |
19288 KB |
Partially correct |
34 |
Partially correct |
1625 ms |
17536 KB |
Partially correct |
35 |
Partially correct |
1630 ms |
17404 KB |
Partially correct |
36 |
Partially correct |
2530 ms |
19284 KB |
Partially correct |
37 |
Partially correct |
1037 ms |
16208 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
600 KB |
Partially correct |
2 |
Partially correct |
0 ms |
344 KB |
Partially correct |
3 |
Partially correct |
0 ms |
348 KB |
Partially correct |
4 |
Partially correct |
0 ms |
344 KB |
Partially correct |
5 |
Partially correct |
0 ms |
348 KB |
Partially correct |
6 |
Partially correct |
0 ms |
468 KB |
Partially correct |
7 |
Partially correct |
1 ms |
348 KB |
Partially correct |
8 |
Partially correct |
1 ms |
348 KB |
Partially correct |
9 |
Partially correct |
1 ms |
348 KB |
Partially correct |
10 |
Partially correct |
0 ms |
348 KB |
Partially correct |
11 |
Partially correct |
0 ms |
456 KB |
Partially correct |
12 |
Partially correct |
1 ms |
348 KB |
Partially correct |
13 |
Partially correct |
0 ms |
348 KB |
Partially correct |
14 |
Partially correct |
0 ms |
348 KB |
Partially correct |
15 |
Partially correct |
0 ms |
348 KB |
Partially correct |
16 |
Partially correct |
1 ms |
348 KB |
Partially correct |
17 |
Partially correct |
1 ms |
344 KB |
Partially correct |
18 |
Partially correct |
1 ms |
348 KB |
Partially correct |
19 |
Partially correct |
0 ms |
348 KB |
Partially correct |
20 |
Partially correct |
0 ms |
348 KB |
Partially correct |
21 |
Partially correct |
0 ms |
348 KB |
Partially correct |
22 |
Partially correct |
0 ms |
348 KB |
Partially correct |
23 |
Partially correct |
1 ms |
348 KB |
Partially correct |
24 |
Partially correct |
0 ms |
348 KB |
Partially correct |
25 |
Partially correct |
2 ms |
860 KB |
Partially correct |
26 |
Partially correct |
13 ms |
868 KB |
Partially correct |
27 |
Partially correct |
23 ms |
900 KB |
Partially correct |
28 |
Partially correct |
21 ms |
1112 KB |
Partially correct |
29 |
Partially correct |
13 ms |
892 KB |
Partially correct |
30 |
Partially correct |
12 ms |
604 KB |
Partially correct |
31 |
Partially correct |
16 ms |
940 KB |
Partially correct |
32 |
Partially correct |
1 ms |
348 KB |
Partially correct |
33 |
Partially correct |
625 ms |
15776 KB |
Partially correct |
34 |
Partially correct |
1022 ms |
18004 KB |
Partially correct |
35 |
Partially correct |
855 ms |
19540 KB |
Partially correct |
36 |
Partially correct |
895 ms |
17956 KB |
Partially correct |
37 |
Partially correct |
908 ms |
17756 KB |
Partially correct |
38 |
Partially correct |
1813 ms |
20052 KB |
Partially correct |
39 |
Partially correct |
1027 ms |
18112 KB |
Partially correct |
40 |
Partially correct |
913 ms |
16464 KB |
Partially correct |
41 |
Partially correct |
927 ms |
16976 KB |
Partially correct |
42 |
Partially correct |
0 ms |
344 KB |
Partially correct |
43 |
Partially correct |
602 ms |
15956 KB |
Partially correct |
44 |
Partially correct |
1016 ms |
18260 KB |
Partially correct |
45 |
Partially correct |
861 ms |
19660 KB |
Partially correct |
46 |
Partially correct |
876 ms |
18004 KB |
Partially correct |
47 |
Partially correct |
927 ms |
17748 KB |
Partially correct |
48 |
Partially correct |
1885 ms |
20048 KB |
Partially correct |
49 |
Partially correct |
1091 ms |
18164 KB |
Partially correct |
50 |
Partially correct |
925 ms |
16592 KB |
Partially correct |
51 |
Partially correct |
917 ms |
16980 KB |
Partially correct |
52 |
Partially correct |
0 ms |
344 KB |
Partially correct |
53 |
Partially correct |
0 ms |
348 KB |
Partially correct |
54 |
Partially correct |
1 ms |
348 KB |
Partially correct |
55 |
Partially correct |
1 ms |
348 KB |
Partially correct |
56 |
Partially correct |
1 ms |
348 KB |
Partially correct |
57 |
Partially correct |
0 ms |
348 KB |
Partially correct |
58 |
Partially correct |
1 ms |
600 KB |
Partially correct |
59 |
Partially correct |
1 ms |
348 KB |
Partially correct |
60 |
Partially correct |
1 ms |
348 KB |
Partially correct |
61 |
Partially correct |
1 ms |
348 KB |
Partially correct |
62 |
Partially correct |
1411 ms |
18060 KB |
Partially correct |
63 |
Partially correct |
884 ms |
18004 KB |
Partially correct |
64 |
Partially correct |
1558 ms |
19288 KB |
Partially correct |
65 |
Partially correct |
1625 ms |
17536 KB |
Partially correct |
66 |
Partially correct |
1630 ms |
17404 KB |
Partially correct |
67 |
Partially correct |
2530 ms |
19284 KB |
Partially correct |
68 |
Partially correct |
1037 ms |
16208 KB |
Partially correct |
69 |
Partially correct |
0 ms |
348 KB |
Partially correct |
70 |
Partially correct |
601 ms |
16072 KB |
Partially correct |
71 |
Partially correct |
975 ms |
18256 KB |
Partially correct |
72 |
Partially correct |
865 ms |
19576 KB |
Partially correct |
73 |
Partially correct |
909 ms |
18000 KB |
Partially correct |
74 |
Partially correct |
920 ms |
17752 KB |
Partially correct |
75 |
Partially correct |
1989 ms |
20308 KB |
Partially correct |
76 |
Partially correct |
1031 ms |
18184 KB |
Partially correct |
77 |
Partially correct |
945 ms |
16728 KB |
Partially correct |
78 |
Partially correct |
928 ms |
17228 KB |
Partially correct |
79 |
Partially correct |
0 ms |
348 KB |
Partially correct |
80 |
Partially correct |
0 ms |
348 KB |
Partially correct |
81 |
Partially correct |
1 ms |
348 KB |
Partially correct |
82 |
Partially correct |
0 ms |
344 KB |
Partially correct |
83 |
Partially correct |
1 ms |
596 KB |
Partially correct |
84 |
Partially correct |
1 ms |
348 KB |
Partially correct |
85 |
Partially correct |
0 ms |
348 KB |
Partially correct |
86 |
Partially correct |
1 ms |
348 KB |
Partially correct |
87 |
Partially correct |
1 ms |
344 KB |
Partially correct |
88 |
Partially correct |
1 ms |
348 KB |
Partially correct |
89 |
Partially correct |
1396 ms |
18232 KB |
Partially correct |
90 |
Partially correct |
873 ms |
17968 KB |
Partially correct |
91 |
Partially correct |
1531 ms |
19028 KB |
Partially correct |
92 |
Partially correct |
1649 ms |
17296 KB |
Partially correct |
93 |
Partially correct |
1664 ms |
17232 KB |
Partially correct |
94 |
Partially correct |
2525 ms |
19284 KB |
Partially correct |
95 |
Partially correct |
1071 ms |
15956 KB |
Partially correct |
96 |
Partially correct |
2 ms |
856 KB |
Partially correct |
97 |
Partially correct |
13 ms |
872 KB |
Partially correct |
98 |
Partially correct |
24 ms |
860 KB |
Partially correct |
99 |
Partially correct |
22 ms |
856 KB |
Partially correct |
100 |
Partially correct |
13 ms |
860 KB |
Partially correct |
101 |
Partially correct |
12 ms |
844 KB |
Partially correct |
102 |
Partially correct |
27 ms |
860 KB |
Partially correct |
103 |
Partially correct |
2144 ms |
21428 KB |
Partially correct |
104 |
Partially correct |
1903 ms |
17440 KB |
Partially correct |
105 |
Partially correct |
1825 ms |
17492 KB |
Partially correct |
106 |
Partially correct |
2472 ms |
17492 KB |
Partially correct |
107 |
Partially correct |
156 ms |
20304 KB |
Partially correct |
108 |
Partially correct |
2131 ms |
18868 KB |
Partially correct |
109 |
Partially correct |
1399 ms |
18384 KB |
Partially correct |
110 |
Partially correct |
1614 ms |
19440 KB |
Partially correct |
111 |
Partially correct |
589 ms |
20228 KB |
Partially correct |
112 |
Partially correct |
1583 ms |
17752 KB |
Partially correct |