// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct segtree{
vector<pair<int,int>> tree;
segtree():tree(524288){}
void update(int k,int x){
k+=262144;
tree[k] = {x,k-262144};
while(k/=2){
tree[k]=max(tree[2*k],tree[2*k+1]);
}
}
int get(int l,int r){
l+=262144;r+=262144;
pair<int,int> ans = {0,0};
while(l<=r){
if(l&1)ans=max(ans,tree[l++]);
if(r%2==0)ans=max(ans,tree[r--]);
l/=2;r/=2;
}
return ans.second;
}
};
pair<int,int> adj[200001];
int tree[200001];
vector<pair<int,int>> chains[200001];
int arr[200001];
int stidx[200001];
int lift[18][200001];
int idx[200001];
int DP[200001];
segtree seg;
int c;
void place(int x,int y,int c){
int node = idx[x];
for(int bit=17;bit>=0;bit--){
if(tree[lift[bit][node]]<y)node = lift[bit][node];
}
chains[node].emplace_back(x,c);
}
void build(int x,int l,int r){
int mid = seg.get(l,r);
idx[mid] = x;
tree[x] = arr[mid];
stidx[x] = mid;
if(mid!=l){
adj[x].first = ++c;
lift[0][c] = x;
build(c,l,mid-1);
}
if(mid!=r){
adj[x].second = ++c;
lift[0][c] = x;
build(c,mid+1,r);
}
}
void merge(pair<int,map<int,int>> &a,pair<int,map<int,int>> &b){
if(a.second.size()<b.second.size())swap(a,b);
int offset = b.first-a.first;
for(auto[x,y]:b.second){
a.second[x] = y+offset;
}
}
pair<int,map<int,int>> calc(int x){
pair<int,map<int,int>> curr = {0,{}};
pair<int,map<int,int>> l = {0,{}},r = {0,{}};
if(adj[x].first)l=calc(adj[x].first);
if(adj[x].second)r=calc(adj[x].second);
l.first+=DP[adj[x].second];
r.first+=DP[adj[x].first];
merge(curr,l);
merge(curr,r);
DP[x] = DP[adj[x].first]+DP[adj[x].second];
curr.second[stidx[x]] = DP[x]-curr.first;
for(auto[a,b]:chains[x]){
DP[x] = max(DP[x],curr.second[a]+curr.first+b);
}
return curr;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for(int i=1;i<=n;i++){cin>>arr[i];seg.update(i,arr[i]);}
build(++c,1,n);
assert(c==n);
for(int bit=1;bit<18;bit++){
for(int i=1;i<=n;i++){
lift[bit][i] = lift[bit-1][lift[bit-1][i]];
}
}
tree[0] = 1e15;
int m;
cin >> m;
int sum = 0;
for(int i=1;i<=m;i++){
int x,y,c;cin>>x>>y>>c;
place(x,y,c);
sum+=c;
}
calc(1);
cout << sum-DP[1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
49500 KB |
Output is correct |
2 |
Correct |
7 ms |
49756 KB |
Output is correct |
3 |
Correct |
6 ms |
49756 KB |
Output is correct |
4 |
Correct |
6 ms |
49756 KB |
Output is correct |
5 |
Correct |
6 ms |
49756 KB |
Output is correct |
6 |
Correct |
5 ms |
49756 KB |
Output is correct |
7 |
Correct |
6 ms |
49628 KB |
Output is correct |
8 |
Correct |
6 ms |
49756 KB |
Output is correct |
9 |
Correct |
6 ms |
49704 KB |
Output is correct |
10 |
Correct |
6 ms |
49756 KB |
Output is correct |
11 |
Correct |
6 ms |
49752 KB |
Output is correct |
12 |
Correct |
6 ms |
49752 KB |
Output is correct |
13 |
Correct |
6 ms |
49756 KB |
Output is correct |
14 |
Correct |
6 ms |
49756 KB |
Output is correct |
15 |
Correct |
5 ms |
49756 KB |
Output is correct |
16 |
Correct |
6 ms |
49704 KB |
Output is correct |
17 |
Correct |
6 ms |
49756 KB |
Output is correct |
18 |
Correct |
5 ms |
49756 KB |
Output is correct |
19 |
Correct |
6 ms |
49756 KB |
Output is correct |
20 |
Correct |
6 ms |
49756 KB |
Output is correct |
21 |
Correct |
5 ms |
49756 KB |
Output is correct |
22 |
Correct |
6 ms |
49756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
49500 KB |
Output is correct |
2 |
Correct |
7 ms |
49756 KB |
Output is correct |
3 |
Correct |
6 ms |
49756 KB |
Output is correct |
4 |
Correct |
6 ms |
49756 KB |
Output is correct |
5 |
Correct |
6 ms |
49756 KB |
Output is correct |
6 |
Correct |
5 ms |
49756 KB |
Output is correct |
7 |
Correct |
6 ms |
49628 KB |
Output is correct |
8 |
Correct |
6 ms |
49756 KB |
Output is correct |
9 |
Correct |
6 ms |
49704 KB |
Output is correct |
10 |
Correct |
6 ms |
49756 KB |
Output is correct |
11 |
Correct |
6 ms |
49752 KB |
Output is correct |
12 |
Correct |
6 ms |
49752 KB |
Output is correct |
13 |
Correct |
6 ms |
49756 KB |
Output is correct |
14 |
Correct |
6 ms |
49756 KB |
Output is correct |
15 |
Correct |
5 ms |
49756 KB |
Output is correct |
16 |
Correct |
6 ms |
49704 KB |
Output is correct |
17 |
Correct |
6 ms |
49756 KB |
Output is correct |
18 |
Correct |
5 ms |
49756 KB |
Output is correct |
19 |
Correct |
6 ms |
49756 KB |
Output is correct |
20 |
Correct |
6 ms |
49756 KB |
Output is correct |
21 |
Correct |
5 ms |
49756 KB |
Output is correct |
22 |
Correct |
6 ms |
49756 KB |
Output is correct |
23 |
Correct |
7 ms |
49704 KB |
Output is correct |
24 |
Correct |
7 ms |
49752 KB |
Output is correct |
25 |
Correct |
10 ms |
49912 KB |
Output is correct |
26 |
Correct |
8 ms |
49752 KB |
Output is correct |
27 |
Correct |
7 ms |
49756 KB |
Output is correct |
28 |
Correct |
8 ms |
49756 KB |
Output is correct |
29 |
Correct |
7 ms |
49756 KB |
Output is correct |
30 |
Correct |
8 ms |
49812 KB |
Output is correct |
31 |
Correct |
7 ms |
49752 KB |
Output is correct |
32 |
Correct |
9 ms |
50268 KB |
Output is correct |
33 |
Correct |
7 ms |
50188 KB |
Output is correct |
34 |
Correct |
7 ms |
50012 KB |
Output is correct |
35 |
Correct |
8 ms |
50000 KB |
Output is correct |
36 |
Correct |
7 ms |
50284 KB |
Output is correct |
37 |
Correct |
7 ms |
50268 KB |
Output is correct |
38 |
Correct |
7 ms |
50524 KB |
Output is correct |
39 |
Correct |
8 ms |
49924 KB |
Output is correct |
40 |
Correct |
7 ms |
50316 KB |
Output is correct |
41 |
Correct |
7 ms |
49924 KB |
Output is correct |
42 |
Correct |
7 ms |
49756 KB |
Output is correct |
43 |
Correct |
8 ms |
50280 KB |
Output is correct |
44 |
Correct |
7 ms |
49756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
49500 KB |
Output is correct |
2 |
Correct |
7 ms |
49756 KB |
Output is correct |
3 |
Correct |
6 ms |
49756 KB |
Output is correct |
4 |
Correct |
6 ms |
49756 KB |
Output is correct |
5 |
Correct |
6 ms |
49756 KB |
Output is correct |
6 |
Correct |
5 ms |
49756 KB |
Output is correct |
7 |
Correct |
6 ms |
49628 KB |
Output is correct |
8 |
Correct |
6 ms |
49756 KB |
Output is correct |
9 |
Correct |
6 ms |
49704 KB |
Output is correct |
10 |
Correct |
6 ms |
49756 KB |
Output is correct |
11 |
Correct |
6 ms |
49752 KB |
Output is correct |
12 |
Correct |
6 ms |
49752 KB |
Output is correct |
13 |
Correct |
6 ms |
49756 KB |
Output is correct |
14 |
Correct |
6 ms |
49756 KB |
Output is correct |
15 |
Correct |
5 ms |
49756 KB |
Output is correct |
16 |
Correct |
6 ms |
49704 KB |
Output is correct |
17 |
Correct |
6 ms |
49756 KB |
Output is correct |
18 |
Correct |
5 ms |
49756 KB |
Output is correct |
19 |
Correct |
6 ms |
49756 KB |
Output is correct |
20 |
Correct |
6 ms |
49756 KB |
Output is correct |
21 |
Correct |
5 ms |
49756 KB |
Output is correct |
22 |
Correct |
6 ms |
49756 KB |
Output is correct |
23 |
Correct |
7 ms |
49704 KB |
Output is correct |
24 |
Correct |
7 ms |
49752 KB |
Output is correct |
25 |
Correct |
10 ms |
49912 KB |
Output is correct |
26 |
Correct |
8 ms |
49752 KB |
Output is correct |
27 |
Correct |
7 ms |
49756 KB |
Output is correct |
28 |
Correct |
8 ms |
49756 KB |
Output is correct |
29 |
Correct |
7 ms |
49756 KB |
Output is correct |
30 |
Correct |
8 ms |
49812 KB |
Output is correct |
31 |
Correct |
7 ms |
49752 KB |
Output is correct |
32 |
Correct |
9 ms |
50268 KB |
Output is correct |
33 |
Correct |
7 ms |
50188 KB |
Output is correct |
34 |
Correct |
7 ms |
50012 KB |
Output is correct |
35 |
Correct |
8 ms |
50000 KB |
Output is correct |
36 |
Correct |
7 ms |
50284 KB |
Output is correct |
37 |
Correct |
7 ms |
50268 KB |
Output is correct |
38 |
Correct |
7 ms |
50524 KB |
Output is correct |
39 |
Correct |
8 ms |
49924 KB |
Output is correct |
40 |
Correct |
7 ms |
50316 KB |
Output is correct |
41 |
Correct |
7 ms |
49924 KB |
Output is correct |
42 |
Correct |
7 ms |
49756 KB |
Output is correct |
43 |
Correct |
8 ms |
50280 KB |
Output is correct |
44 |
Correct |
7 ms |
49756 KB |
Output is correct |
45 |
Correct |
300 ms |
74740 KB |
Output is correct |
46 |
Correct |
283 ms |
73296 KB |
Output is correct |
47 |
Correct |
284 ms |
73812 KB |
Output is correct |
48 |
Correct |
265 ms |
73268 KB |
Output is correct |
49 |
Correct |
268 ms |
71420 KB |
Output is correct |
50 |
Correct |
289 ms |
75244 KB |
Output is correct |
51 |
Correct |
249 ms |
72788 KB |
Output is correct |
52 |
Correct |
277 ms |
73300 KB |
Output is correct |
53 |
Correct |
267 ms |
73316 KB |
Output is correct |
54 |
Correct |
300 ms |
117072 KB |
Output is correct |
55 |
Correct |
335 ms |
104020 KB |
Output is correct |
56 |
Correct |
278 ms |
96792 KB |
Output is correct |
57 |
Correct |
282 ms |
91612 KB |
Output is correct |
58 |
Correct |
258 ms |
102228 KB |
Output is correct |
59 |
Correct |
254 ms |
101968 KB |
Output is correct |
60 |
Correct |
210 ms |
141648 KB |
Output is correct |
61 |
Correct |
256 ms |
73288 KB |
Output is correct |
62 |
Correct |
308 ms |
123088 KB |
Output is correct |
63 |
Correct |
238 ms |
70088 KB |
Output is correct |
64 |
Correct |
239 ms |
73588 KB |
Output is correct |
65 |
Correct |
325 ms |
124244 KB |
Output is correct |
66 |
Correct |
231 ms |
70372 KB |
Output is correct |