-
Notifications
You must be signed in to change notification settings - Fork 2
/
q.cpp
73 lines (58 loc) · 1.23 KB
/
q.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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 1e9 + 7;
ll int INF = 1e17;
int cost[200005];
int harr[200005];
ll int dp[200005];
ll int seg[800005];
void update(int node, int start, int end, int l, int r, ll int val)
{
if(end < l || start > r)
return;
if(start == end)
seg[node] = val;
else
{
int mid = (start + end) / 2;
update(node*2, start, mid, l, r, val);
update(node*2 + 1, mid+1, end, l, r, val);
seg[node] = max(seg[node*2], seg[node*2+1]);
}
return;
}
ll int query(int node, int start, int end, int l, int r)
{
if(end < l || start > r)
return 0;
if(l <= start && end <= r)
return seg[node];
ll int q1, q2;
int mid = (start + end) / 2;
q1 = query(node*2, start, mid, l, r);
q2 = query(node*2+1, mid+1, end, l, r);
return max(q1, q2);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
ll int y;
cin>>n;
for(int i=0;i<n;i++)
cin>>harr[i];
for(int i=0;i<n;i++)
cin>>cost[i];
for(int i=0;i<n;i++)
{
y = query(1, 0, n, 0, harr[i]);
dp[harr[i]] = y + cost[i];
update(1, 0, n, harr[i], harr[i], dp[harr[i]]);
}
for(int i=0;i<=n;i++)
y = max(y, dp[i]);
cout<<y<<endl;
return 0;
}