#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
#include <stack>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int maxn = 200001;
int n, m;
int a[maxn], l[maxn], r[maxn];
ll dp[maxn];
map<int, ll> ans[maxn];
vector<pi> p[maxn];
vector<int> stk;
void add(int x, int y, ll z){
if((--ans[x].upper_bound(y))->s >= z) return;
ans[x][y] = z;
auto it = ans[x].upper_bound(y);
while(it != ans[x].end() && it->s <= z) it = ans[x].erase(it);
}
void dfs(int c){
if(!c) return;
int l = ::l[c], r = ::r[c];
dfs(l), dfs(r);
ll x = dp[l] + (--ans[l].upper_bound(a[c]))->s;
ll y = dp[r] + (--ans[r].upper_bound(a[c]))->s;
dp[l] += y, dp[r] += x;
if(ans[l].size() > ans[r].size()) swap(l, r);
if(r) swap(ans[c], ans[r]), swap(dp[c], dp[r]);
dp[0] = 0;
for(auto i : ans[l]) add(c, i.f, i.s + dp[l] - dp[c]);
for(auto i : p[c]) add(c, i.f, i.s + x + y - dp[c]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
ans[0][1] = 0;
for(int i = 1; i <= n; i++){
ans[i][1] = 0;
cin >> a[i];
while(!stk.empty() && a[stk.back()] < a[i]){
l[i] = stk.back();
stk.pop_back();
}
if(!stk.empty()) r[stk.back()] = i;
stk.push_back(i);
}
cin >> m;
ll ret = 0;
for(int i = 0; i < m; i++){
int x, y, z;
cin >> x >> y >> z;
ret += z;
p[x].push_back({y, z});
}
dfs(stk[0]);
ret -= dp[stk[0]] + (--ans[stk[0]].end())->s;
cout << ret << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
14 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
11 |
Correct |
12 ms |
14592 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14592 KB |
Output is correct |
14 |
Correct |
14 ms |
14464 KB |
Output is correct |
15 |
Correct |
12 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
12 ms |
14464 KB |
Output is correct |
18 |
Correct |
12 ms |
14464 KB |
Output is correct |
19 |
Correct |
12 ms |
14464 KB |
Output is correct |
20 |
Correct |
12 ms |
14464 KB |
Output is correct |
21 |
Correct |
12 ms |
14464 KB |
Output is correct |
22 |
Correct |
12 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
14 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
11 |
Correct |
12 ms |
14592 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14592 KB |
Output is correct |
14 |
Correct |
14 ms |
14464 KB |
Output is correct |
15 |
Correct |
12 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
12 ms |
14464 KB |
Output is correct |
18 |
Correct |
12 ms |
14464 KB |
Output is correct |
19 |
Correct |
12 ms |
14464 KB |
Output is correct |
20 |
Correct |
12 ms |
14464 KB |
Output is correct |
21 |
Correct |
12 ms |
14464 KB |
Output is correct |
22 |
Correct |
12 ms |
14464 KB |
Output is correct |
23 |
Correct |
13 ms |
14720 KB |
Output is correct |
24 |
Correct |
15 ms |
14848 KB |
Output is correct |
25 |
Correct |
15 ms |
14720 KB |
Output is correct |
26 |
Correct |
14 ms |
14720 KB |
Output is correct |
27 |
Correct |
14 ms |
14720 KB |
Output is correct |
28 |
Correct |
13 ms |
14720 KB |
Output is correct |
29 |
Correct |
13 ms |
14720 KB |
Output is correct |
30 |
Correct |
13 ms |
14720 KB |
Output is correct |
31 |
Correct |
14 ms |
14752 KB |
Output is correct |
32 |
Correct |
13 ms |
14848 KB |
Output is correct |
33 |
Correct |
13 ms |
14848 KB |
Output is correct |
34 |
Correct |
13 ms |
14720 KB |
Output is correct |
35 |
Correct |
14 ms |
14848 KB |
Output is correct |
36 |
Correct |
13 ms |
14848 KB |
Output is correct |
37 |
Correct |
13 ms |
14848 KB |
Output is correct |
38 |
Correct |
13 ms |
14976 KB |
Output is correct |
39 |
Correct |
13 ms |
14720 KB |
Output is correct |
40 |
Correct |
13 ms |
14848 KB |
Output is correct |
41 |
Correct |
18 ms |
14720 KB |
Output is correct |
42 |
Correct |
13 ms |
14720 KB |
Output is correct |
43 |
Correct |
14 ms |
14848 KB |
Output is correct |
44 |
Correct |
13 ms |
14720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14464 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
14 ms |
14464 KB |
Output is correct |
7 |
Correct |
12 ms |
14464 KB |
Output is correct |
8 |
Correct |
12 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14464 KB |
Output is correct |
10 |
Correct |
11 ms |
14464 KB |
Output is correct |
11 |
Correct |
12 ms |
14592 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14592 KB |
Output is correct |
14 |
Correct |
14 ms |
14464 KB |
Output is correct |
15 |
Correct |
12 ms |
14464 KB |
Output is correct |
16 |
Correct |
13 ms |
14464 KB |
Output is correct |
17 |
Correct |
12 ms |
14464 KB |
Output is correct |
18 |
Correct |
12 ms |
14464 KB |
Output is correct |
19 |
Correct |
12 ms |
14464 KB |
Output is correct |
20 |
Correct |
12 ms |
14464 KB |
Output is correct |
21 |
Correct |
12 ms |
14464 KB |
Output is correct |
22 |
Correct |
12 ms |
14464 KB |
Output is correct |
23 |
Correct |
13 ms |
14720 KB |
Output is correct |
24 |
Correct |
15 ms |
14848 KB |
Output is correct |
25 |
Correct |
15 ms |
14720 KB |
Output is correct |
26 |
Correct |
14 ms |
14720 KB |
Output is correct |
27 |
Correct |
14 ms |
14720 KB |
Output is correct |
28 |
Correct |
13 ms |
14720 KB |
Output is correct |
29 |
Correct |
13 ms |
14720 KB |
Output is correct |
30 |
Correct |
13 ms |
14720 KB |
Output is correct |
31 |
Correct |
14 ms |
14752 KB |
Output is correct |
32 |
Correct |
13 ms |
14848 KB |
Output is correct |
33 |
Correct |
13 ms |
14848 KB |
Output is correct |
34 |
Correct |
13 ms |
14720 KB |
Output is correct |
35 |
Correct |
14 ms |
14848 KB |
Output is correct |
36 |
Correct |
13 ms |
14848 KB |
Output is correct |
37 |
Correct |
13 ms |
14848 KB |
Output is correct |
38 |
Correct |
13 ms |
14976 KB |
Output is correct |
39 |
Correct |
13 ms |
14720 KB |
Output is correct |
40 |
Correct |
13 ms |
14848 KB |
Output is correct |
41 |
Correct |
18 ms |
14720 KB |
Output is correct |
42 |
Correct |
13 ms |
14720 KB |
Output is correct |
43 |
Correct |
14 ms |
14848 KB |
Output is correct |
44 |
Correct |
13 ms |
14720 KB |
Output is correct |
45 |
Correct |
206 ms |
40184 KB |
Output is correct |
46 |
Correct |
210 ms |
39672 KB |
Output is correct |
47 |
Correct |
211 ms |
39672 KB |
Output is correct |
48 |
Correct |
215 ms |
39900 KB |
Output is correct |
49 |
Correct |
200 ms |
39416 KB |
Output is correct |
50 |
Correct |
201 ms |
39288 KB |
Output is correct |
51 |
Correct |
203 ms |
39544 KB |
Output is correct |
52 |
Correct |
209 ms |
39928 KB |
Output is correct |
53 |
Correct |
204 ms |
39804 KB |
Output is correct |
54 |
Correct |
212 ms |
55108 KB |
Output is correct |
55 |
Correct |
197 ms |
51188 KB |
Output is correct |
56 |
Correct |
202 ms |
48888 KB |
Output is correct |
57 |
Correct |
216 ms |
47584 KB |
Output is correct |
58 |
Correct |
185 ms |
46960 KB |
Output is correct |
59 |
Correct |
188 ms |
46704 KB |
Output is correct |
60 |
Correct |
257 ms |
64632 KB |
Output is correct |
61 |
Correct |
230 ms |
46712 KB |
Output is correct |
62 |
Correct |
289 ms |
58352 KB |
Output is correct |
63 |
Correct |
225 ms |
44688 KB |
Output is correct |
64 |
Correct |
227 ms |
45816 KB |
Output is correct |
65 |
Correct |
284 ms |
58740 KB |
Output is correct |
66 |
Correct |
221 ms |
45176 KB |
Output is correct |