#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=2e5;
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, ~rc?dp2[rc]:0);
ds3.upd(m2, r, ~lc?dp2[lc]:0);
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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 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 |
416 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 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 |
416 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
4 ms |
512 KB |
Output is correct |
26 |
Correct |
4 ms |
512 KB |
Output is correct |
27 |
Correct |
3 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 |
4 ms |
640 KB |
Output is correct |
34 |
Correct |
4 ms |
640 KB |
Output is correct |
35 |
Correct |
4 ms |
640 KB |
Output is correct |
36 |
Correct |
3 ms |
768 KB |
Output is correct |
37 |
Correct |
4 ms |
640 KB |
Output is correct |
38 |
Correct |
3 ms |
896 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
4 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 |
4 ms |
768 KB |
Output is correct |
44 |
Correct |
4 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 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 |
416 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
4 ms |
512 KB |
Output is correct |
26 |
Correct |
4 ms |
512 KB |
Output is correct |
27 |
Correct |
3 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 |
4 ms |
640 KB |
Output is correct |
34 |
Correct |
4 ms |
640 KB |
Output is correct |
35 |
Correct |
4 ms |
640 KB |
Output is correct |
36 |
Correct |
3 ms |
768 KB |
Output is correct |
37 |
Correct |
4 ms |
640 KB |
Output is correct |
38 |
Correct |
3 ms |
896 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
4 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 |
4 ms |
768 KB |
Output is correct |
44 |
Correct |
4 ms |
512 KB |
Output is correct |
45 |
Correct |
270 ms |
14400 KB |
Output is correct |
46 |
Correct |
236 ms |
14072 KB |
Output is correct |
47 |
Correct |
254 ms |
14456 KB |
Output is correct |
48 |
Correct |
276 ms |
14412 KB |
Output is correct |
49 |
Correct |
238 ms |
14072 KB |
Output is correct |
50 |
Correct |
237 ms |
13944 KB |
Output is correct |
51 |
Correct |
239 ms |
14200 KB |
Output is correct |
52 |
Correct |
243 ms |
14584 KB |
Output is correct |
53 |
Correct |
238 ms |
14200 KB |
Output is correct |
54 |
Correct |
374 ms |
35192 KB |
Output is correct |
55 |
Correct |
356 ms |
29564 KB |
Output is correct |
56 |
Correct |
344 ms |
26208 KB |
Output is correct |
57 |
Correct |
337 ms |
24008 KB |
Output is correct |
58 |
Correct |
326 ms |
28624 KB |
Output is correct |
59 |
Correct |
345 ms |
28688 KB |
Output is correct |
60 |
Correct |
257 ms |
45816 KB |
Output is correct |
61 |
Correct |
256 ms |
15224 KB |
Output is correct |
62 |
Correct |
401 ms |
38520 KB |
Output is correct |
63 |
Correct |
257 ms |
15352 KB |
Output is correct |
64 |
Correct |
256 ms |
14968 KB |
Output is correct |
65 |
Correct |
399 ms |
38836 KB |
Output is correct |
66 |
Correct |
260 ms |
15224 KB |
Output is correct |