#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<map<int,int>> DP(n+1);
vector<int> H(n+1);
vector<int> C(n+1);
vector<int> p(n+1);
vector<int> ind(n+1);
for(int i=1;i<=n;i++){
int a;
cin>>a>>H[i]>>C[i];
adj[a].emplace_back(i);
ind[a]++;p[i]=a;
}
queue<int> q;
vector<bool> visited(n+1);
for(int i=1;i<=n;i++)if(ind[i]==0)q.emplace(i);
while(!q.empty()){
auto x = q.front();q.pop();
visited[x]=true;
DP[x][0]=C[x];
DP[x][H[x]]=-C[x];
DP[x][H[x]+1]=C[x];
for(int&i:adj[x]){
if(DP[x].size()<DP[i].size())swap(DP[x],DP[i]);
for(auto[a,b]:DP[i]){
DP[x][a]+=b;
if(DP[x][a]==0)DP[x].erase(a);
}
}
auto iter = DP[x].find(H[x]);
while(iter!=DP[x].end()){
if(iter->second>0)break;
int val = iter->second;
iter = DP[x].erase(iter);iter--;
iter->second+=val;
}
if(--ind[p[x]]==0)q.emplace(p[x]);
}
int ans = 0;
for(int par=1;par<=n;par++){
if(visited[par])continue;
function<void(int)> dfs = [&](int x){
if(visited[x])return;
visited[x]=true;
DP[x][0]=C[x];
DP[x][H[x]]=-C[x];
DP[x][H[x]+1]=C[x];
for(int&i:adj[x])if(i!=par){
dfs(i);
if(DP[x].size()<DP[i].size())swap(DP[x],DP[i]);
for(auto[a,b]:DP[i]){
DP[x][a]+=b;
if(DP[x][a]==0)DP[x].erase(a);
}
}
};
dfs(par);
auto curr = DP[par];
int currans = 1e15;
int currsum = 0;
if(curr[0]==0)continue;
for(auto[a,b]:curr){
currsum+=b;
currans = min(currans,currsum);
}
ans+=currans;
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
4 ms |
1880 KB |
Output is correct |
7 |
Correct |
5 ms |
1756 KB |
Output is correct |
8 |
Correct |
6 ms |
2648 KB |
Output is correct |
9 |
Correct |
4 ms |
1884 KB |
Output is correct |
10 |
Correct |
4 ms |
1628 KB |
Output is correct |
11 |
Correct |
2 ms |
1368 KB |
Output is correct |
12 |
Correct |
4 ms |
1884 KB |
Output is correct |
13 |
Correct |
3 ms |
1884 KB |
Output is correct |
14 |
Correct |
4 ms |
1884 KB |
Output is correct |
15 |
Correct |
4 ms |
1884 KB |
Output is correct |
16 |
Correct |
6 ms |
2984 KB |
Output is correct |
17 |
Correct |
4 ms |
2156 KB |
Output is correct |
18 |
Correct |
3 ms |
1372 KB |
Output is correct |
19 |
Correct |
6 ms |
1880 KB |
Output is correct |
20 |
Correct |
3 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
4 ms |
1756 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
4 ms |
1884 KB |
Output is correct |
25 |
Correct |
3 ms |
1628 KB |
Output is correct |
26 |
Correct |
2 ms |
1628 KB |
Output is correct |
27 |
Correct |
4 ms |
1884 KB |
Output is correct |
28 |
Correct |
4 ms |
2140 KB |
Output is correct |
29 |
Correct |
4 ms |
2396 KB |
Output is correct |
30 |
Correct |
5 ms |
2736 KB |
Output is correct |
31 |
Correct |
5 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
4 ms |
1880 KB |
Output is correct |
7 |
Correct |
5 ms |
1756 KB |
Output is correct |
8 |
Correct |
6 ms |
2648 KB |
Output is correct |
9 |
Correct |
4 ms |
1884 KB |
Output is correct |
10 |
Correct |
4 ms |
1628 KB |
Output is correct |
11 |
Correct |
2 ms |
1368 KB |
Output is correct |
12 |
Correct |
4 ms |
1884 KB |
Output is correct |
13 |
Correct |
3 ms |
1884 KB |
Output is correct |
14 |
Correct |
4 ms |
1884 KB |
Output is correct |
15 |
Correct |
4 ms |
1884 KB |
Output is correct |
16 |
Correct |
6 ms |
2984 KB |
Output is correct |
17 |
Correct |
4 ms |
2156 KB |
Output is correct |
18 |
Correct |
3 ms |
1372 KB |
Output is correct |
19 |
Correct |
6 ms |
1880 KB |
Output is correct |
20 |
Correct |
3 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
4 ms |
1756 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
4 ms |
1884 KB |
Output is correct |
25 |
Correct |
3 ms |
1628 KB |
Output is correct |
26 |
Correct |
2 ms |
1628 KB |
Output is correct |
27 |
Correct |
4 ms |
1884 KB |
Output is correct |
28 |
Correct |
4 ms |
2140 KB |
Output is correct |
29 |
Correct |
4 ms |
2396 KB |
Output is correct |
30 |
Correct |
5 ms |
2736 KB |
Output is correct |
31 |
Correct |
5 ms |
2652 KB |
Output is correct |
32 |
Correct |
6 ms |
2652 KB |
Output is correct |
33 |
Correct |
352 ms |
103912 KB |
Output is correct |
34 |
Correct |
255 ms |
69456 KB |
Output is correct |
35 |
Correct |
349 ms |
101204 KB |
Output is correct |
36 |
Correct |
259 ms |
69484 KB |
Output is correct |
37 |
Correct |
176 ms |
49236 KB |
Output is correct |
38 |
Correct |
126 ms |
36968 KB |
Output is correct |
39 |
Correct |
176 ms |
64596 KB |
Output is correct |
40 |
Correct |
164 ms |
64728 KB |
Output is correct |
41 |
Correct |
98 ms |
64596 KB |
Output is correct |
42 |
Correct |
193 ms |
64752 KB |
Output is correct |
43 |
Correct |
179 ms |
64740 KB |
Output is correct |
44 |
Correct |
329 ms |
129104 KB |
Output is correct |
45 |
Correct |
212 ms |
84320 KB |
Output is correct |
46 |
Correct |
74 ms |
36436 KB |
Output is correct |
47 |
Correct |
241 ms |
62860 KB |
Output is correct |
48 |
Correct |
127 ms |
48980 KB |
Output is correct |
49 |
Correct |
85 ms |
48968 KB |
Output is correct |
50 |
Correct |
193 ms |
60000 KB |
Output is correct |
51 |
Correct |
82 ms |
35020 KB |
Output is correct |
52 |
Correct |
217 ms |
63940 KB |
Output is correct |
53 |
Correct |
121 ms |
49864 KB |
Output is correct |
54 |
Correct |
88 ms |
52048 KB |
Output is correct |
55 |
Correct |
232 ms |
68176 KB |
Output is correct |
56 |
Correct |
189 ms |
76620 KB |
Output is correct |
57 |
Correct |
190 ms |
81232 KB |
Output is correct |
58 |
Correct |
343 ms |
95820 KB |
Output is correct |
59 |
Correct |
326 ms |
95704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
348 KB |
Output is correct |
5 |
Correct |
6 ms |
2652 KB |
Output is correct |
6 |
Correct |
4 ms |
1880 KB |
Output is correct |
7 |
Correct |
5 ms |
1756 KB |
Output is correct |
8 |
Correct |
6 ms |
2648 KB |
Output is correct |
9 |
Correct |
4 ms |
1884 KB |
Output is correct |
10 |
Correct |
4 ms |
1628 KB |
Output is correct |
11 |
Correct |
2 ms |
1368 KB |
Output is correct |
12 |
Correct |
4 ms |
1884 KB |
Output is correct |
13 |
Correct |
3 ms |
1884 KB |
Output is correct |
14 |
Correct |
4 ms |
1884 KB |
Output is correct |
15 |
Correct |
4 ms |
1884 KB |
Output is correct |
16 |
Correct |
6 ms |
2984 KB |
Output is correct |
17 |
Correct |
4 ms |
2156 KB |
Output is correct |
18 |
Correct |
3 ms |
1372 KB |
Output is correct |
19 |
Correct |
6 ms |
1880 KB |
Output is correct |
20 |
Correct |
3 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
4 ms |
1756 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
4 ms |
1884 KB |
Output is correct |
25 |
Correct |
3 ms |
1628 KB |
Output is correct |
26 |
Correct |
2 ms |
1628 KB |
Output is correct |
27 |
Correct |
4 ms |
1884 KB |
Output is correct |
28 |
Correct |
4 ms |
2140 KB |
Output is correct |
29 |
Correct |
4 ms |
2396 KB |
Output is correct |
30 |
Correct |
5 ms |
2736 KB |
Output is correct |
31 |
Correct |
5 ms |
2652 KB |
Output is correct |
32 |
Correct |
6 ms |
2652 KB |
Output is correct |
33 |
Correct |
352 ms |
103912 KB |
Output is correct |
34 |
Correct |
255 ms |
69456 KB |
Output is correct |
35 |
Correct |
349 ms |
101204 KB |
Output is correct |
36 |
Correct |
259 ms |
69484 KB |
Output is correct |
37 |
Correct |
176 ms |
49236 KB |
Output is correct |
38 |
Correct |
126 ms |
36968 KB |
Output is correct |
39 |
Correct |
176 ms |
64596 KB |
Output is correct |
40 |
Correct |
164 ms |
64728 KB |
Output is correct |
41 |
Correct |
98 ms |
64596 KB |
Output is correct |
42 |
Correct |
193 ms |
64752 KB |
Output is correct |
43 |
Correct |
179 ms |
64740 KB |
Output is correct |
44 |
Correct |
329 ms |
129104 KB |
Output is correct |
45 |
Correct |
212 ms |
84320 KB |
Output is correct |
46 |
Correct |
74 ms |
36436 KB |
Output is correct |
47 |
Correct |
241 ms |
62860 KB |
Output is correct |
48 |
Correct |
127 ms |
48980 KB |
Output is correct |
49 |
Correct |
85 ms |
48968 KB |
Output is correct |
50 |
Correct |
193 ms |
60000 KB |
Output is correct |
51 |
Correct |
82 ms |
35020 KB |
Output is correct |
52 |
Correct |
217 ms |
63940 KB |
Output is correct |
53 |
Correct |
121 ms |
49864 KB |
Output is correct |
54 |
Correct |
88 ms |
52048 KB |
Output is correct |
55 |
Correct |
232 ms |
68176 KB |
Output is correct |
56 |
Correct |
189 ms |
76620 KB |
Output is correct |
57 |
Correct |
190 ms |
81232 KB |
Output is correct |
58 |
Correct |
343 ms |
95820 KB |
Output is correct |
59 |
Correct |
326 ms |
95704 KB |
Output is correct |
60 |
Correct |
0 ms |
344 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
418 ms |
99124 KB |
Output is correct |
65 |
Correct |
341 ms |
74460 KB |
Output is correct |
66 |
Correct |
347 ms |
96340 KB |
Output is correct |
67 |
Correct |
302 ms |
74420 KB |
Output is correct |
68 |
Correct |
182 ms |
58912 KB |
Output is correct |
69 |
Correct |
197 ms |
42912 KB |
Output is correct |
70 |
Correct |
396 ms |
122196 KB |
Output is correct |
71 |
Correct |
251 ms |
82852 KB |
Output is correct |
72 |
Correct |
464 ms |
144812 KB |
Output is correct |
73 |
Correct |
257 ms |
95052 KB |
Output is correct |
74 |
Correct |
371 ms |
107224 KB |
Output is correct |
75 |
Correct |
203 ms |
69972 KB |
Output is correct |
76 |
Correct |
168 ms |
69204 KB |
Output is correct |
77 |
Correct |
328 ms |
108744 KB |
Output is correct |
78 |
Correct |
204 ms |
67528 KB |
Output is correct |
79 |
Correct |
409 ms |
130640 KB |
Output is correct |
80 |
Correct |
248 ms |
86644 KB |
Output is correct |
81 |
Correct |
179 ms |
74832 KB |
Output is correct |
82 |
Correct |
374 ms |
144972 KB |
Output is correct |
83 |
Correct |
95 ms |
69696 KB |
Output is correct |
84 |
Correct |
379 ms |
119280 KB |
Output is correct |
85 |
Correct |
412 ms |
119376 KB |
Output is correct |
86 |
Correct |
323 ms |
100884 KB |
Output is correct |
87 |
Correct |
409 ms |
119280 KB |
Output is correct |
88 |
Correct |
372 ms |
119336 KB |
Output is correct |