-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-on-both-sides.cpp
101 lines (97 loc) · 1.97 KB
/
number-on-both-sides.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define len(__x) (int) __x.size()
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vll2d = vector<vll>;
using vi = vector<int>;
using vi2d = vector<vi>;
using pii = pair<int, int>;
using vii = vector<pii>;
using vc = vector<char>;
#define all(a) a.begin(), a.end()
#define snd second
#define fst first
#define pb(___x) push_back(___x)
#define mp(___a, ___b) make_pair(___a, ___b)
#define eb(___x) emplace_back(___x)
const ll INF = 1e18;
int n;
int k, l;
vll a(1e5+1);
vll b(1e5+1);
ll sumfront = 0, sumback = 0;
multiset<ll> sfront, sback, candidate;
void add(const int p) {
sumfront += a[p];
sfront.insert(a[p]);
if(len(sback) < l) {
sumback += b[p];
sback.insert(b[p]);
}
else {
if(*sback.begin() < b[p]) {
auto v = *sback.begin();
sback.erase(sback.find(v));
sumback -= v;
sumback += b[p];
sback.insert(b[p]);
candidate.insert(v);
}
else candidate.insert(b[p]);
}
}
void remove(const int p) {
sumfront -= a[p];
sfront.erase(sfront.find(a[p]));
if (sback.find(b[p]) != sback.end()) {
sumback -= b[p];
sback.erase(sback.find(b[p]));
auto it = candidate.rbegin();
sback.insert(*it);
sumback += *it;
candidate.erase(candidate.find(*it));
}
else {
candidate.erase(candidate.find(b[p]));
}
}
void run() {
cin >> n;
for(int i = 0; i < n; i++) {
cin >> a[i];
}
for(int i = 0; i < n; i++) {
cin >> b[i];
}
cin >> k >> l;
for(int i = 0; i < k; i++) {
add(i);
}
int pl = k-1;
int pr = n-1;
ll ans = sumfront + sumback;
while(pl >= 0) {
remove(pl);
add(pr);
ans = max(ans, sumfront+sumback);
pl--, pr--;
}
ans = max(ans, sumfront+sumback);
cout << ans << endl;
}
int32_t main(void) {
// fastio;
int t;
t = 1;
// cin >> t;
while (t--) run();
}
// AC, greedy, data structures