#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);
swap(ans[c], ans[r]);
swap(dp[c], dp[r]);
dp[0] = 0;
ans[0].clear();
ans[0][1] = 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 |
14 ms |
14592 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 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 |
14464 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14592 KB |
Output is correct |
14 |
Correct |
12 ms |
14592 KB |
Output is correct |
15 |
Correct |
12 ms |
14720 KB |
Output is correct |
16 |
Correct |
12 ms |
14592 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 |
14 ms |
14592 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 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 |
14464 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14592 KB |
Output is correct |
14 |
Correct |
12 ms |
14592 KB |
Output is correct |
15 |
Correct |
12 ms |
14720 KB |
Output is correct |
16 |
Correct |
12 ms |
14592 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 |
14 ms |
14720 KB |
Output is correct |
24 |
Correct |
14 ms |
14720 KB |
Output is correct |
25 |
Correct |
14 ms |
14720 KB |
Output is correct |
26 |
Correct |
13 ms |
14720 KB |
Output is correct |
27 |
Correct |
14 ms |
14720 KB |
Output is correct |
28 |
Correct |
14 ms |
14720 KB |
Output is correct |
29 |
Correct |
16 ms |
14720 KB |
Output is correct |
30 |
Correct |
13 ms |
14848 KB |
Output is correct |
31 |
Correct |
14 ms |
14720 KB |
Output is correct |
32 |
Correct |
13 ms |
14976 KB |
Output is correct |
33 |
Correct |
13 ms |
14848 KB |
Output is correct |
34 |
Correct |
13 ms |
14848 KB |
Output is correct |
35 |
Correct |
14 ms |
14848 KB |
Output is correct |
36 |
Correct |
13 ms |
14848 KB |
Output is correct |
37 |
Correct |
14 ms |
14848 KB |
Output is correct |
38 |
Correct |
13 ms |
15104 KB |
Output is correct |
39 |
Correct |
14 ms |
14720 KB |
Output is correct |
40 |
Correct |
13 ms |
14976 KB |
Output is correct |
41 |
Correct |
13 ms |
14720 KB |
Output is correct |
42 |
Correct |
15 ms |
14976 KB |
Output is correct |
43 |
Correct |
14 ms |
14976 KB |
Output is correct |
44 |
Correct |
13 ms |
14848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14464 KB |
Output is correct |
2 |
Correct |
14 ms |
14592 KB |
Output is correct |
3 |
Correct |
12 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14464 KB |
Output is correct |
5 |
Correct |
12 ms |
14464 KB |
Output is correct |
6 |
Correct |
12 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 |
14464 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
12 ms |
14592 KB |
Output is correct |
14 |
Correct |
12 ms |
14592 KB |
Output is correct |
15 |
Correct |
12 ms |
14720 KB |
Output is correct |
16 |
Correct |
12 ms |
14592 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 |
14 ms |
14720 KB |
Output is correct |
24 |
Correct |
14 ms |
14720 KB |
Output is correct |
25 |
Correct |
14 ms |
14720 KB |
Output is correct |
26 |
Correct |
13 ms |
14720 KB |
Output is correct |
27 |
Correct |
14 ms |
14720 KB |
Output is correct |
28 |
Correct |
14 ms |
14720 KB |
Output is correct |
29 |
Correct |
16 ms |
14720 KB |
Output is correct |
30 |
Correct |
13 ms |
14848 KB |
Output is correct |
31 |
Correct |
14 ms |
14720 KB |
Output is correct |
32 |
Correct |
13 ms |
14976 KB |
Output is correct |
33 |
Correct |
13 ms |
14848 KB |
Output is correct |
34 |
Correct |
13 ms |
14848 KB |
Output is correct |
35 |
Correct |
14 ms |
14848 KB |
Output is correct |
36 |
Correct |
13 ms |
14848 KB |
Output is correct |
37 |
Correct |
14 ms |
14848 KB |
Output is correct |
38 |
Correct |
13 ms |
15104 KB |
Output is correct |
39 |
Correct |
14 ms |
14720 KB |
Output is correct |
40 |
Correct |
13 ms |
14976 KB |
Output is correct |
41 |
Correct |
13 ms |
14720 KB |
Output is correct |
42 |
Correct |
15 ms |
14976 KB |
Output is correct |
43 |
Correct |
14 ms |
14976 KB |
Output is correct |
44 |
Correct |
13 ms |
14848 KB |
Output is correct |
45 |
Correct |
223 ms |
45688 KB |
Output is correct |
46 |
Correct |
235 ms |
45304 KB |
Output is correct |
47 |
Correct |
228 ms |
45376 KB |
Output is correct |
48 |
Correct |
220 ms |
45432 KB |
Output is correct |
49 |
Correct |
223 ms |
44920 KB |
Output is correct |
50 |
Correct |
225 ms |
44920 KB |
Output is correct |
51 |
Correct |
210 ms |
44920 KB |
Output is correct |
52 |
Correct |
219 ms |
45688 KB |
Output is correct |
53 |
Correct |
226 ms |
45476 KB |
Output is correct |
54 |
Correct |
210 ms |
66928 KB |
Output is correct |
55 |
Correct |
226 ms |
61432 KB |
Output is correct |
56 |
Correct |
234 ms |
58100 KB |
Output is correct |
57 |
Correct |
224 ms |
55928 KB |
Output is correct |
58 |
Correct |
205 ms |
56944 KB |
Output is correct |
59 |
Correct |
209 ms |
56964 KB |
Output is correct |
60 |
Correct |
271 ms |
78488 KB |
Output is correct |
61 |
Correct |
258 ms |
51832 KB |
Output is correct |
62 |
Correct |
309 ms |
71232 KB |
Output is correct |
63 |
Correct |
233 ms |
49748 KB |
Output is correct |
64 |
Correct |
243 ms |
50808 KB |
Output is correct |
65 |
Correct |
311 ms |
71664 KB |
Output is correct |
66 |
Correct |
229 ms |
50424 KB |
Output is correct |