#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=3e5;
int n, m, a[mxN];
ar<int, 3> b[mxN];
ll s, dp2[mxN];
struct ds1 {
ar<int, 2> st[2*mxN];
void init() {
for(int i=0; i<n; ++i)
st[i+n]={a[i], i};
for(int i=n-1; i; --i)
st[i]=max(st[2*i], st[2*i+1]);
}
ar<int, 2> qry(int l, int r) {
ar<int, 2> b{};
for(l+=n, r+=n+1; l<r; ++l/=2, r/=2) {
if(l&1)
b=max(st[l], b);
if(r&1)
b=max(st[r-1], b);
}
return b;
}
} ds1;
struct ds2 {
ar<int, 3> st[2*mxN];
void init() {
for(int i=0; i<m; ++i)
st[i+m]={b[i][1], b[i][0], b[i][2]};
for(int i=m-1; i; --i)
st[i]=min(st[2*i], st[2*i+1]);
}
ar<int, 3> qry(int l, int r) {
ar<int, 3> b{n+1};
for(l+=m, r+=m+1; l<r; ++l/=2, r/=2) {
if(l&1)
b=min(st[l], b);
if(r&1)
b=min(st[r-1], b);
}
return b;
}
void upd(int i, ar<int, 3> x) {
for(st[i+=m]=x; i/=2; )
st[i]=min(st[2*i], st[2*i+1]);
}
} ds2;
struct ds3 {
ll ft[mxN+1];
void upd(int i, ll x) {
for(++i; i<=n; i+=i&-i)
ft[i]+=x;
}
void upd(int l, int r, ll x) {
upd(l, x);
upd(r+1, -x);
}
ll qry(int i) {
ll r=0;
for(++i; i; i-=i&-i)
r+=ft[i];
return r;
}
} ds3;
int dfs(int l, int r, int mx) {
if(l>r)
return -1;
int m2=ds1.qry(l, r)[1];
int lc=dfs(l, m2-1, a[m2]), rc=dfs(m2+1, r, a[m2]);
dp2[m2]=(~lc?dp2[lc]:0)+(~rc?dp2[rc]:0);
int l2=lower_bound(b, b+m, ar<int, 3>{l})-b, r2=lower_bound(b, b+m, ar<int, 3>{r+1})-b-1;
while(1) {
ar<int, 3> d=ds2.qry(l2, r2);
if(d[0]>mx)
break;
if(d[1]<m2) {
dp2[m2]=max(d[2]+(~lc?ds3.qry(d[1]):0)+(~rc?dp2[rc]:0), dp2[m2]);
} else if(d[1]>m2) {
dp2[m2]=max(d[2]+(~lc?dp2[lc]:0)+(~rc?ds3.qry(d[1]):0), dp2[m2]);
} else {
dp2[m2]=max(d[2]+(~lc?dp2[lc]:0)+(~rc?dp2[rc]:0), dp2[m2]);
}
ds2.upd(lower_bound(b, b+m, ar<int, 3>{d[1], d[0], d[2]})-b, {n+1});
}
ds3.upd(l, m2, dp2[rc]);
ds3.upd(m2, r, dp2[lc]);
return m2;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i=0; i<n; ++i)
cin >> a[i];
ds1.init();
cin >> m;
for(int i=0; i<m; ++i) {
cin >> b[i][0] >> b[i][1] >> b[i][2], --b[i][0];
s+=b[i][2];
}
sort(b, b+m);
ds2.init();
int rt=dfs(0, n-1, n);
cout << s-dp2[rt];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
4 ms |
512 KB |
Output is correct |
25 |
Correct |
4 ms |
544 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
4 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
4 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
768 KB |
Output is correct |
33 |
Correct |
5 ms |
768 KB |
Output is correct |
34 |
Correct |
3 ms |
640 KB |
Output is correct |
35 |
Correct |
4 ms |
640 KB |
Output is correct |
36 |
Correct |
4 ms |
768 KB |
Output is correct |
37 |
Correct |
5 ms |
768 KB |
Output is correct |
38 |
Correct |
4 ms |
896 KB |
Output is correct |
39 |
Correct |
4 ms |
512 KB |
Output is correct |
40 |
Correct |
3 ms |
768 KB |
Output is correct |
41 |
Correct |
3 ms |
512 KB |
Output is correct |
42 |
Correct |
3 ms |
512 KB |
Output is correct |
43 |
Correct |
3 ms |
768 KB |
Output is correct |
44 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
4 ms |
512 KB |
Output is correct |
25 |
Correct |
4 ms |
544 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
4 ms |
512 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
3 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
512 KB |
Output is correct |
31 |
Correct |
4 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
768 KB |
Output is correct |
33 |
Correct |
5 ms |
768 KB |
Output is correct |
34 |
Correct |
3 ms |
640 KB |
Output is correct |
35 |
Correct |
4 ms |
640 KB |
Output is correct |
36 |
Correct |
4 ms |
768 KB |
Output is correct |
37 |
Correct |
5 ms |
768 KB |
Output is correct |
38 |
Correct |
4 ms |
896 KB |
Output is correct |
39 |
Correct |
4 ms |
512 KB |
Output is correct |
40 |
Correct |
3 ms |
768 KB |
Output is correct |
41 |
Correct |
3 ms |
512 KB |
Output is correct |
42 |
Correct |
3 ms |
512 KB |
Output is correct |
43 |
Correct |
3 ms |
768 KB |
Output is correct |
44 |
Correct |
3 ms |
640 KB |
Output is correct |
45 |
Correct |
260 ms |
19876 KB |
Output is correct |
46 |
Correct |
252 ms |
19704 KB |
Output is correct |
47 |
Correct |
268 ms |
19960 KB |
Output is correct |
48 |
Correct |
249 ms |
19576 KB |
Output is correct |
49 |
Correct |
251 ms |
19576 KB |
Output is correct |
50 |
Correct |
247 ms |
19372 KB |
Output is correct |
51 |
Correct |
254 ms |
19504 KB |
Output is correct |
52 |
Correct |
258 ms |
19964 KB |
Output is correct |
53 |
Correct |
269 ms |
19576 KB |
Output is correct |
54 |
Correct |
415 ms |
42872 KB |
Output is correct |
55 |
Correct |
403 ms |
36636 KB |
Output is correct |
56 |
Correct |
400 ms |
32760 KB |
Output is correct |
57 |
Correct |
406 ms |
30576 KB |
Output is correct |
58 |
Correct |
349 ms |
35324 KB |
Output is correct |
59 |
Correct |
344 ms |
35696 KB |
Output is correct |
60 |
Correct |
278 ms |
53460 KB |
Output is correct |
61 |
Correct |
284 ms |
19408 KB |
Output is correct |
62 |
Correct |
399 ms |
45400 KB |
Output is correct |
63 |
Correct |
255 ms |
19388 KB |
Output is correct |
64 |
Correct |
261 ms |
18876 KB |
Output is correct |
65 |
Correct |
413 ms |
45568 KB |
Output is correct |
66 |
Correct |
273 ms |
19576 KB |
Output is correct |