-
Notifications
You must be signed in to change notification settings - Fork 2
/
Q - Flowers.cpp
48 lines (41 loc) · 914 Bytes
/
Q - Flowers.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
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mxN = 1e6+6;
int seg[2*mxN], N;
void update(int k, int x) {
k += N; seg[k] = x; k >>= 1;
while (k > 0) {
seg[k] = max(seg[2*k], seg[2*k+1]);
k >>= 1;
}
}
int query(int a, int b) {
a += N, b += N;
int s = 0;
while (a <= b) {
if (a&1) s = max(s, seg[a++]);
if (b&1^1) s = max(s, seg[b--]);
a >>= 1, b >>= 1;
}
return s;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
N = 1LL<< (int) ceil(log2(n));
vector<int> a(n), b(n);
for (auto &i: a)
cin >> i;
for (auto &i: b)
cin >> i;
int ans = b[0];
update(a[0]-1, b[0]);
for (int i = 1; i < n; i++) {
int x = b[i] + query(0, a[i]-1);
ans = max(ans, x);
update(a[i]-1, x);
}
cout << ans;
}