#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> adj(n+1);
vector<int> H(n+1);
vector<int> C(n+1);
int a;cin>>a>>H[1]>>C[1];
for(int i=2;i<=n;i++){
cin>>a>>H[i]>>C[i];
adj[a].emplace_back(i);
}
function<map<int,int>(int)> dfs = [&](int x){
map<int,int> DP;
DP[0]=C[x];
DP[H[x]]=-C[x];
DP[H[x]+1]=C[x];
for(int&i:adj[x]){
auto curr = dfs(i);
if(DP.size()<curr.size())swap(DP,curr);
for(auto[a,b]:curr){
DP[a]+=b;
if(DP[a]==0)DP.erase(a);
}
}
auto iter = DP.find(H[x]);
while(iter!=DP.end()){
if(iter->second>0)break;
int val = iter->second;
iter = DP.erase(iter);iter--;
iter->second+=val;
}
return DP;
};
cout << dfs(1)[0] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
1116 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
5 ms |
1112 KB |
Output is correct |
9 |
Correct |
4 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
4 ms |
2952 KB |
Output is correct |
13 |
Correct |
3 ms |
2828 KB |
Output is correct |
14 |
Correct |
4 ms |
1884 KB |
Output is correct |
15 |
Correct |
3 ms |
1980 KB |
Output is correct |
16 |
Correct |
5 ms |
1116 KB |
Output is correct |
17 |
Correct |
4 ms |
1112 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
4 ms |
1908 KB |
Output is correct |
20 |
Correct |
3 ms |
1880 KB |
Output is correct |
21 |
Correct |
3 ms |
1804 KB |
Output is correct |
22 |
Correct |
3 ms |
1036 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
4 ms |
1884 KB |
Output is correct |
25 |
Correct |
3 ms |
1884 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
4 ms |
1628 KB |
Output is correct |
28 |
Correct |
4 ms |
1884 KB |
Output is correct |
29 |
Correct |
4 ms |
2328 KB |
Output is correct |
30 |
Correct |
4 ms |
1884 KB |
Output is correct |
31 |
Correct |
4 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
1116 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
5 ms |
1112 KB |
Output is correct |
9 |
Correct |
4 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
4 ms |
2952 KB |
Output is correct |
13 |
Correct |
3 ms |
2828 KB |
Output is correct |
14 |
Correct |
4 ms |
1884 KB |
Output is correct |
15 |
Correct |
3 ms |
1980 KB |
Output is correct |
16 |
Correct |
5 ms |
1116 KB |
Output is correct |
17 |
Correct |
4 ms |
1112 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
4 ms |
1908 KB |
Output is correct |
20 |
Correct |
3 ms |
1880 KB |
Output is correct |
21 |
Correct |
3 ms |
1804 KB |
Output is correct |
22 |
Correct |
3 ms |
1036 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
4 ms |
1884 KB |
Output is correct |
25 |
Correct |
3 ms |
1884 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
4 ms |
1628 KB |
Output is correct |
28 |
Correct |
4 ms |
1884 KB |
Output is correct |
29 |
Correct |
4 ms |
2328 KB |
Output is correct |
30 |
Correct |
4 ms |
1884 KB |
Output is correct |
31 |
Correct |
4 ms |
1884 KB |
Output is correct |
32 |
Correct |
5 ms |
1116 KB |
Output is correct |
33 |
Correct |
238 ms |
29116 KB |
Output is correct |
34 |
Correct |
152 ms |
17236 KB |
Output is correct |
35 |
Correct |
237 ms |
27216 KB |
Output is correct |
36 |
Correct |
157 ms |
17236 KB |
Output is correct |
37 |
Correct |
97 ms |
16980 KB |
Output is correct |
38 |
Correct |
77 ms |
16756 KB |
Output is correct |
39 |
Correct |
183 ms |
104020 KB |
Output is correct |
40 |
Correct |
170 ms |
104112 KB |
Output is correct |
41 |
Correct |
119 ms |
104020 KB |
Output is correct |
42 |
Correct |
175 ms |
61884 KB |
Output is correct |
43 |
Correct |
157 ms |
61936 KB |
Output is correct |
44 |
Correct |
261 ms |
28596 KB |
Output is correct |
45 |
Correct |
179 ms |
16724 KB |
Output is correct |
46 |
Correct |
65 ms |
16212 KB |
Output is correct |
47 |
Correct |
215 ms |
58572 KB |
Output is correct |
48 |
Correct |
129 ms |
58652 KB |
Output is correct |
49 |
Correct |
101 ms |
58708 KB |
Output is correct |
50 |
Correct |
130 ms |
26600 KB |
Output is correct |
51 |
Correct |
68 ms |
14028 KB |
Output is correct |
52 |
Correct |
218 ms |
65736 KB |
Output is correct |
53 |
Correct |
128 ms |
59592 KB |
Output is correct |
54 |
Correct |
132 ms |
104272 KB |
Output is correct |
55 |
Correct |
162 ms |
58708 KB |
Output is correct |
56 |
Correct |
176 ms |
80720 KB |
Output is correct |
57 |
Correct |
174 ms |
89428 KB |
Output is correct |
58 |
Correct |
211 ms |
67412 KB |
Output is correct |
59 |
Correct |
215 ms |
67364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
1116 KB |
Output is correct |
6 |
Correct |
3 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
5 ms |
1112 KB |
Output is correct |
9 |
Correct |
4 ms |
860 KB |
Output is correct |
10 |
Correct |
3 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
4 ms |
2952 KB |
Output is correct |
13 |
Correct |
3 ms |
2828 KB |
Output is correct |
14 |
Correct |
4 ms |
1884 KB |
Output is correct |
15 |
Correct |
3 ms |
1980 KB |
Output is correct |
16 |
Correct |
5 ms |
1116 KB |
Output is correct |
17 |
Correct |
4 ms |
1112 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
4 ms |
1908 KB |
Output is correct |
20 |
Correct |
3 ms |
1880 KB |
Output is correct |
21 |
Correct |
3 ms |
1804 KB |
Output is correct |
22 |
Correct |
3 ms |
1036 KB |
Output is correct |
23 |
Correct |
2 ms |
860 KB |
Output is correct |
24 |
Correct |
4 ms |
1884 KB |
Output is correct |
25 |
Correct |
3 ms |
1884 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
4 ms |
1628 KB |
Output is correct |
28 |
Correct |
4 ms |
1884 KB |
Output is correct |
29 |
Correct |
4 ms |
2328 KB |
Output is correct |
30 |
Correct |
4 ms |
1884 KB |
Output is correct |
31 |
Correct |
4 ms |
1884 KB |
Output is correct |
32 |
Correct |
5 ms |
1116 KB |
Output is correct |
33 |
Correct |
238 ms |
29116 KB |
Output is correct |
34 |
Correct |
152 ms |
17236 KB |
Output is correct |
35 |
Correct |
237 ms |
27216 KB |
Output is correct |
36 |
Correct |
157 ms |
17236 KB |
Output is correct |
37 |
Correct |
97 ms |
16980 KB |
Output is correct |
38 |
Correct |
77 ms |
16756 KB |
Output is correct |
39 |
Correct |
183 ms |
104020 KB |
Output is correct |
40 |
Correct |
170 ms |
104112 KB |
Output is correct |
41 |
Correct |
119 ms |
104020 KB |
Output is correct |
42 |
Correct |
175 ms |
61884 KB |
Output is correct |
43 |
Correct |
157 ms |
61936 KB |
Output is correct |
44 |
Correct |
261 ms |
28596 KB |
Output is correct |
45 |
Correct |
179 ms |
16724 KB |
Output is correct |
46 |
Correct |
65 ms |
16212 KB |
Output is correct |
47 |
Correct |
215 ms |
58572 KB |
Output is correct |
48 |
Correct |
129 ms |
58652 KB |
Output is correct |
49 |
Correct |
101 ms |
58708 KB |
Output is correct |
50 |
Correct |
130 ms |
26600 KB |
Output is correct |
51 |
Correct |
68 ms |
14028 KB |
Output is correct |
52 |
Correct |
218 ms |
65736 KB |
Output is correct |
53 |
Correct |
128 ms |
59592 KB |
Output is correct |
54 |
Correct |
132 ms |
104272 KB |
Output is correct |
55 |
Correct |
162 ms |
58708 KB |
Output is correct |
56 |
Correct |
176 ms |
80720 KB |
Output is correct |
57 |
Correct |
174 ms |
89428 KB |
Output is correct |
58 |
Correct |
211 ms |
67412 KB |
Output is correct |
59 |
Correct |
215 ms |
67364 KB |
Output is correct |
60 |
Correct |
1 ms |
344 KB |
Output is correct |
61 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |