#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) x.size())
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x, y) cerr << #x << " is " << x << ", " << #y << " is " << y << endl;
typedef long long lint;
typedef pair<lint, lint> ii;
vector<int> adj[200005];
map<int, lint> dp[200005];
lint cost[200005];
lint original[200005];
void dfs(int u){
for(int v : adj[u]){
dfs(v);
if(sz(dp[u]) < sz(dp[v])) swap(dp[u], dp[v]);
map<int, lint> &cur = dp[u];
for(ii x : dp[v]){
if(cur.count(x.first)) cur[x.first] += x.second;
else cur[x.first] = x.second;
}
}
lint O = original[u]; lint C = cost[u];
map<int, lint> &cur = dp[u];
if(cur.count(O)) cur[O] += C ;
else cur[O] = C;
while(true){
auto it = cur.upper_bound(O);
if(it == cur.end()) break;
if(it->second > C){
it->second -= C;
break;
}
else{
C -= it->second;
cur.erase(it);
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
lint ans = 0;
int n; cin >> n;
for(int i = 1;i <= n;i++){
int a; cin >> a >> original[i] >> cost[i];
original[i] *= -1;
if(i != a) adj[a].push_back(i);
ans += cost[i];
}
dfs(1);
for(ii x : dp[1]) ans -= x.second;
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
10 ms |
14404 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14316 KB |
Output is correct |
5 |
Correct |
15 ms |
15664 KB |
Output is correct |
6 |
Correct |
37 ms |
15176 KB |
Output is correct |
7 |
Correct |
13 ms |
14944 KB |
Output is correct |
8 |
Correct |
16 ms |
15668 KB |
Output is correct |
9 |
Correct |
15 ms |
15180 KB |
Output is correct |
10 |
Correct |
14 ms |
15004 KB |
Output is correct |
11 |
Correct |
12 ms |
14868 KB |
Output is correct |
12 |
Correct |
13 ms |
15308 KB |
Output is correct |
13 |
Correct |
13 ms |
15328 KB |
Output is correct |
14 |
Correct |
14 ms |
15116 KB |
Output is correct |
15 |
Correct |
13 ms |
15052 KB |
Output is correct |
16 |
Correct |
18 ms |
15968 KB |
Output is correct |
17 |
Correct |
18 ms |
15436 KB |
Output is correct |
18 |
Correct |
12 ms |
14840 KB |
Output is correct |
19 |
Correct |
14 ms |
15308 KB |
Output is correct |
20 |
Correct |
12 ms |
15052 KB |
Output is correct |
21 |
Correct |
12 ms |
15112 KB |
Output is correct |
22 |
Correct |
14 ms |
15200 KB |
Output is correct |
23 |
Correct |
14 ms |
14944 KB |
Output is correct |
24 |
Correct |
17 ms |
15328 KB |
Output is correct |
25 |
Correct |
12 ms |
15196 KB |
Output is correct |
26 |
Correct |
12 ms |
15416 KB |
Output is correct |
27 |
Correct |
16 ms |
15352 KB |
Output is correct |
28 |
Correct |
13 ms |
15328 KB |
Output is correct |
29 |
Correct |
13 ms |
15456 KB |
Output is correct |
30 |
Correct |
13 ms |
15444 KB |
Output is correct |
31 |
Correct |
14 ms |
15544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
15636 KB |
Output is correct |
2 |
Correct |
456 ms |
80180 KB |
Output is correct |
3 |
Correct |
355 ms |
55364 KB |
Output is correct |
4 |
Correct |
445 ms |
77200 KB |
Output is correct |
5 |
Correct |
334 ms |
55364 KB |
Output is correct |
6 |
Correct |
221 ms |
36416 KB |
Output is correct |
7 |
Correct |
191 ms |
31752 KB |
Output is correct |
8 |
Correct |
179 ms |
54040 KB |
Output is correct |
9 |
Correct |
160 ms |
53880 KB |
Output is correct |
10 |
Correct |
112 ms |
53880 KB |
Output is correct |
11 |
Correct |
169 ms |
41592 KB |
Output is correct |
12 |
Correct |
160 ms |
41540 KB |
Output is correct |
13 |
Correct |
429 ms |
105344 KB |
Output is correct |
14 |
Correct |
288 ms |
69444 KB |
Output is correct |
15 |
Correct |
103 ms |
31916 KB |
Output is correct |
16 |
Correct |
288 ms |
51448 KB |
Output is correct |
17 |
Correct |
140 ms |
44484 KB |
Output is correct |
18 |
Correct |
109 ms |
44484 KB |
Output is correct |
19 |
Correct |
328 ms |
47664 KB |
Output is correct |
20 |
Correct |
113 ms |
35132 KB |
Output is correct |
21 |
Correct |
343 ms |
52080 KB |
Output is correct |
22 |
Correct |
134 ms |
44984 KB |
Output is correct |
23 |
Correct |
118 ms |
54080 KB |
Output is correct |
24 |
Correct |
249 ms |
53096 KB |
Output is correct |
25 |
Correct |
212 ms |
58308 KB |
Output is correct |
26 |
Correct |
194 ms |
60728 KB |
Output is correct |
27 |
Correct |
244 ms |
59536 KB |
Output is correct |
28 |
Correct |
238 ms |
59656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
10 ms |
14404 KB |
Output is correct |
3 |
Correct |
9 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14316 KB |
Output is correct |
5 |
Correct |
15 ms |
15664 KB |
Output is correct |
6 |
Correct |
37 ms |
15176 KB |
Output is correct |
7 |
Correct |
13 ms |
14944 KB |
Output is correct |
8 |
Correct |
16 ms |
15668 KB |
Output is correct |
9 |
Correct |
15 ms |
15180 KB |
Output is correct |
10 |
Correct |
14 ms |
15004 KB |
Output is correct |
11 |
Correct |
12 ms |
14868 KB |
Output is correct |
12 |
Correct |
13 ms |
15308 KB |
Output is correct |
13 |
Correct |
13 ms |
15328 KB |
Output is correct |
14 |
Correct |
14 ms |
15116 KB |
Output is correct |
15 |
Correct |
13 ms |
15052 KB |
Output is correct |
16 |
Correct |
18 ms |
15968 KB |
Output is correct |
17 |
Correct |
18 ms |
15436 KB |
Output is correct |
18 |
Correct |
12 ms |
14840 KB |
Output is correct |
19 |
Correct |
14 ms |
15308 KB |
Output is correct |
20 |
Correct |
12 ms |
15052 KB |
Output is correct |
21 |
Correct |
12 ms |
15112 KB |
Output is correct |
22 |
Correct |
14 ms |
15200 KB |
Output is correct |
23 |
Correct |
14 ms |
14944 KB |
Output is correct |
24 |
Correct |
17 ms |
15328 KB |
Output is correct |
25 |
Correct |
12 ms |
15196 KB |
Output is correct |
26 |
Correct |
12 ms |
15416 KB |
Output is correct |
27 |
Correct |
16 ms |
15352 KB |
Output is correct |
28 |
Correct |
13 ms |
15328 KB |
Output is correct |
29 |
Correct |
13 ms |
15456 KB |
Output is correct |
30 |
Correct |
13 ms |
15444 KB |
Output is correct |
31 |
Correct |
14 ms |
15544 KB |
Output is correct |
32 |
Correct |
17 ms |
15636 KB |
Output is correct |
33 |
Correct |
456 ms |
80180 KB |
Output is correct |
34 |
Correct |
355 ms |
55364 KB |
Output is correct |
35 |
Correct |
445 ms |
77200 KB |
Output is correct |
36 |
Correct |
334 ms |
55364 KB |
Output is correct |
37 |
Correct |
221 ms |
36416 KB |
Output is correct |
38 |
Correct |
191 ms |
31752 KB |
Output is correct |
39 |
Correct |
179 ms |
54040 KB |
Output is correct |
40 |
Correct |
160 ms |
53880 KB |
Output is correct |
41 |
Correct |
112 ms |
53880 KB |
Output is correct |
42 |
Correct |
169 ms |
41592 KB |
Output is correct |
43 |
Correct |
160 ms |
41540 KB |
Output is correct |
44 |
Correct |
429 ms |
105344 KB |
Output is correct |
45 |
Correct |
288 ms |
69444 KB |
Output is correct |
46 |
Correct |
103 ms |
31916 KB |
Output is correct |
47 |
Correct |
288 ms |
51448 KB |
Output is correct |
48 |
Correct |
140 ms |
44484 KB |
Output is correct |
49 |
Correct |
109 ms |
44484 KB |
Output is correct |
50 |
Correct |
328 ms |
47664 KB |
Output is correct |
51 |
Correct |
113 ms |
35132 KB |
Output is correct |
52 |
Correct |
343 ms |
52080 KB |
Output is correct |
53 |
Correct |
134 ms |
44984 KB |
Output is correct |
54 |
Correct |
118 ms |
54080 KB |
Output is correct |
55 |
Correct |
249 ms |
53096 KB |
Output is correct |
56 |
Correct |
212 ms |
58308 KB |
Output is correct |
57 |
Correct |
194 ms |
60728 KB |
Output is correct |
58 |
Correct |
244 ms |
59536 KB |
Output is correct |
59 |
Correct |
238 ms |
59656 KB |
Output is correct |
60 |
Runtime error |
249 ms |
524292 KB |
Execution killed with signal 9 |
61 |
Halted |
0 ms |
0 KB |
- |