#include <bits/stdc++.h>
using namespace std;
const int nx=2e5+5;
#define ll long long
ll n, pa[nx], h[nx], c[nx], vs[nx], ans, cy[nx];
vector<ll> d[nx], g[nx];
map<ll, ll> mp[nx], dp[nx];
bool dfscy(int u, int rt)
{
//cout<<"dfscy "<<u<<'\n';
vs[u]=1;
bool res=0;
for (auto v:d[u])
{
if (vs[v]==2) continue;
else if (!vs[v]) res=res||dfscy(v, rt);
else return g[rt].push_back(u), cy[u]=1, 1;
}
if (res) g[rt].push_back(u), cy[u]=1;
return res;
}
void dfsfill(int u)
{
vs[u]=2;
for (auto v:d[u]) if (vs[u]!=2) dfsfill(v);
}
void dfs(int u)
{
mp[u][0]=c[u];
for (auto v:d[u])
{
if (cy[v]) continue;
dfs(v);
if (mp[v].size()>mp[u].size()) swap(mp[u], mp[v]);
for (auto [x, y]:mp[v]) mp[u][x]+=y;
}
if (cy[u]) return;
mp[u][h[u]]+=0;
mp[u][h[u]+1]+=c[u];
auto itr=mp[u].find(h[u]);
ll sm=-c[u], lst=h[u];
while (1)
{
sm+=itr->second;
if (sm>=0) break;
itr=mp[u].erase(itr);
itr--;
}
mp[u][itr->first]=sm;
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>pa[i]>>h[i]>>c[i];
for (int i=1; i<=n; i++) d[pa[i]].push_back(i);
for (int i=1; i<=n; i++)
{
if (!vs[i])
{
if (dfscy(i, i))
{
map<ll, ll> cst;
for (auto u:g[i])
{
cst[h[u]]+=c[u];
dfs(u);
if (mp[u].size()>dp[i].size()) swap(mp[u], dp[i]);
for (auto [x, y]:mp[u]) dp[i][x]+=y;
}
//for (auto x:mp[i]) cout<<x.first<<' '<<x.second<<'\n';
ll cur=dp[i].begin()->second, tmp=0;
auto itr=dp[i].begin();
for (auto [x, y]:cst)
{
while (itr!=dp[i].end()&&itr->first<=x) tmp+=itr->second, itr++;
//cout<<"cst "<<x<<' '<<tmp<<' '<<tmp-y<<'\n';
cur=min(cur, tmp-y);
}
//cout<<"cur "<<cur<<'\n';
ans+=cur;
}
dfsfill(i);
}
}
/*
cout<<"cy ";
for (int i=1; i<=n; i++) cout<<cy[i]<<' ';
cout<<'\n';
*/
cout<<ans;
}
/*
3
3 1 1
1 1 1
2 1 1
*/
Compilation message
worst_reporter2.cpp: In function 'void dfs(int)':
worst_reporter2.cpp:48:18: warning: unused variable 'lst' [-Wunused-variable]
48 | ll sm=-c[u], lst=h[u];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
28504 KB |
Output is correct |
2 |
Correct |
12 ms |
28504 KB |
Output is correct |
3 |
Correct |
14 ms |
28464 KB |
Output is correct |
4 |
Correct |
17 ms |
28504 KB |
Output is correct |
5 |
Correct |
17 ms |
30296 KB |
Output is correct |
6 |
Correct |
17 ms |
29784 KB |
Output is correct |
7 |
Correct |
16 ms |
29528 KB |
Output is correct |
8 |
Correct |
17 ms |
30296 KB |
Output is correct |
9 |
Correct |
17 ms |
29788 KB |
Output is correct |
10 |
Correct |
18 ms |
29468 KB |
Output is correct |
11 |
Correct |
14 ms |
29276 KB |
Output is correct |
12 |
Correct |
19 ms |
30044 KB |
Output is correct |
13 |
Correct |
16 ms |
30044 KB |
Output is correct |
14 |
Correct |
15 ms |
29788 KB |
Output is correct |
15 |
Correct |
14 ms |
29744 KB |
Output is correct |
16 |
Correct |
17 ms |
30556 KB |
Output is correct |
17 |
Correct |
16 ms |
30024 KB |
Output is correct |
18 |
Correct |
14 ms |
29364 KB |
Output is correct |
19 |
Correct |
15 ms |
29788 KB |
Output is correct |
20 |
Correct |
15 ms |
29788 KB |
Output is correct |
21 |
Correct |
14 ms |
29788 KB |
Output is correct |
22 |
Correct |
15 ms |
29788 KB |
Output is correct |
23 |
Correct |
15 ms |
29616 KB |
Output is correct |
24 |
Correct |
16 ms |
30044 KB |
Output is correct |
25 |
Correct |
15 ms |
29788 KB |
Output is correct |
26 |
Correct |
19 ms |
30396 KB |
Output is correct |
27 |
Correct |
17 ms |
30044 KB |
Output is correct |
28 |
Correct |
15 ms |
30044 KB |
Output is correct |
29 |
Correct |
15 ms |
30044 KB |
Output is correct |
30 |
Correct |
16 ms |
30040 KB |
Output is correct |
31 |
Correct |
16 ms |
30040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
28504 KB |
Output is correct |
2 |
Correct |
12 ms |
28504 KB |
Output is correct |
3 |
Correct |
14 ms |
28464 KB |
Output is correct |
4 |
Correct |
17 ms |
28504 KB |
Output is correct |
5 |
Correct |
17 ms |
30296 KB |
Output is correct |
6 |
Correct |
17 ms |
29784 KB |
Output is correct |
7 |
Correct |
16 ms |
29528 KB |
Output is correct |
8 |
Correct |
17 ms |
30296 KB |
Output is correct |
9 |
Correct |
17 ms |
29788 KB |
Output is correct |
10 |
Correct |
18 ms |
29468 KB |
Output is correct |
11 |
Correct |
14 ms |
29276 KB |
Output is correct |
12 |
Correct |
19 ms |
30044 KB |
Output is correct |
13 |
Correct |
16 ms |
30044 KB |
Output is correct |
14 |
Correct |
15 ms |
29788 KB |
Output is correct |
15 |
Correct |
14 ms |
29744 KB |
Output is correct |
16 |
Correct |
17 ms |
30556 KB |
Output is correct |
17 |
Correct |
16 ms |
30024 KB |
Output is correct |
18 |
Correct |
14 ms |
29364 KB |
Output is correct |
19 |
Correct |
15 ms |
29788 KB |
Output is correct |
20 |
Correct |
15 ms |
29788 KB |
Output is correct |
21 |
Correct |
14 ms |
29788 KB |
Output is correct |
22 |
Correct |
15 ms |
29788 KB |
Output is correct |
23 |
Correct |
15 ms |
29616 KB |
Output is correct |
24 |
Correct |
16 ms |
30044 KB |
Output is correct |
25 |
Correct |
15 ms |
29788 KB |
Output is correct |
26 |
Correct |
19 ms |
30396 KB |
Output is correct |
27 |
Correct |
17 ms |
30044 KB |
Output is correct |
28 |
Correct |
15 ms |
30044 KB |
Output is correct |
29 |
Correct |
15 ms |
30044 KB |
Output is correct |
30 |
Correct |
16 ms |
30040 KB |
Output is correct |
31 |
Correct |
16 ms |
30040 KB |
Output is correct |
32 |
Correct |
21 ms |
30288 KB |
Output is correct |
33 |
Correct |
323 ms |
110420 KB |
Output is correct |
34 |
Correct |
247 ms |
85588 KB |
Output is correct |
35 |
Correct |
316 ms |
107344 KB |
Output is correct |
36 |
Correct |
243 ms |
85588 KB |
Output is correct |
37 |
Correct |
202 ms |
66748 KB |
Output is correct |
38 |
Correct |
153 ms |
62236 KB |
Output is correct |
39 |
Correct |
184 ms |
86900 KB |
Output is correct |
40 |
Correct |
158 ms |
86868 KB |
Output is correct |
41 |
Correct |
108 ms |
86772 KB |
Output is correct |
42 |
Correct |
152 ms |
73044 KB |
Output is correct |
43 |
Correct |
143 ms |
72784 KB |
Output is correct |
44 |
Correct |
301 ms |
134876 KB |
Output is correct |
45 |
Correct |
217 ms |
99228 KB |
Output is correct |
46 |
Correct |
99 ms |
61788 KB |
Output is correct |
47 |
Correct |
220 ms |
82668 KB |
Output is correct |
48 |
Correct |
128 ms |
75856 KB |
Output is correct |
49 |
Correct |
109 ms |
75932 KB |
Output is correct |
50 |
Correct |
199 ms |
78276 KB |
Output is correct |
51 |
Correct |
101 ms |
65732 KB |
Output is correct |
52 |
Correct |
236 ms |
83908 KB |
Output is correct |
53 |
Correct |
122 ms |
76744 KB |
Output is correct |
54 |
Correct |
202 ms |
99676 KB |
Output is correct |
55 |
Correct |
186 ms |
84968 KB |
Output is correct |
56 |
Correct |
157 ms |
90448 KB |
Output is correct |
57 |
Correct |
174 ms |
93392 KB |
Output is correct |
58 |
Correct |
171 ms |
90960 KB |
Output is correct |
59 |
Correct |
168 ms |
90968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
28504 KB |
Output is correct |
2 |
Correct |
12 ms |
28504 KB |
Output is correct |
3 |
Correct |
14 ms |
28464 KB |
Output is correct |
4 |
Correct |
17 ms |
28504 KB |
Output is correct |
5 |
Correct |
17 ms |
30296 KB |
Output is correct |
6 |
Correct |
17 ms |
29784 KB |
Output is correct |
7 |
Correct |
16 ms |
29528 KB |
Output is correct |
8 |
Correct |
17 ms |
30296 KB |
Output is correct |
9 |
Correct |
17 ms |
29788 KB |
Output is correct |
10 |
Correct |
18 ms |
29468 KB |
Output is correct |
11 |
Correct |
14 ms |
29276 KB |
Output is correct |
12 |
Correct |
19 ms |
30044 KB |
Output is correct |
13 |
Correct |
16 ms |
30044 KB |
Output is correct |
14 |
Correct |
15 ms |
29788 KB |
Output is correct |
15 |
Correct |
14 ms |
29744 KB |
Output is correct |
16 |
Correct |
17 ms |
30556 KB |
Output is correct |
17 |
Correct |
16 ms |
30024 KB |
Output is correct |
18 |
Correct |
14 ms |
29364 KB |
Output is correct |
19 |
Correct |
15 ms |
29788 KB |
Output is correct |
20 |
Correct |
15 ms |
29788 KB |
Output is correct |
21 |
Correct |
14 ms |
29788 KB |
Output is correct |
22 |
Correct |
15 ms |
29788 KB |
Output is correct |
23 |
Correct |
15 ms |
29616 KB |
Output is correct |
24 |
Correct |
16 ms |
30044 KB |
Output is correct |
25 |
Correct |
15 ms |
29788 KB |
Output is correct |
26 |
Correct |
19 ms |
30396 KB |
Output is correct |
27 |
Correct |
17 ms |
30044 KB |
Output is correct |
28 |
Correct |
15 ms |
30044 KB |
Output is correct |
29 |
Correct |
15 ms |
30044 KB |
Output is correct |
30 |
Correct |
16 ms |
30040 KB |
Output is correct |
31 |
Correct |
16 ms |
30040 KB |
Output is correct |
32 |
Correct |
21 ms |
30288 KB |
Output is correct |
33 |
Correct |
323 ms |
110420 KB |
Output is correct |
34 |
Correct |
247 ms |
85588 KB |
Output is correct |
35 |
Correct |
316 ms |
107344 KB |
Output is correct |
36 |
Correct |
243 ms |
85588 KB |
Output is correct |
37 |
Correct |
202 ms |
66748 KB |
Output is correct |
38 |
Correct |
153 ms |
62236 KB |
Output is correct |
39 |
Correct |
184 ms |
86900 KB |
Output is correct |
40 |
Correct |
158 ms |
86868 KB |
Output is correct |
41 |
Correct |
108 ms |
86772 KB |
Output is correct |
42 |
Correct |
152 ms |
73044 KB |
Output is correct |
43 |
Correct |
143 ms |
72784 KB |
Output is correct |
44 |
Correct |
301 ms |
134876 KB |
Output is correct |
45 |
Correct |
217 ms |
99228 KB |
Output is correct |
46 |
Correct |
99 ms |
61788 KB |
Output is correct |
47 |
Correct |
220 ms |
82668 KB |
Output is correct |
48 |
Correct |
128 ms |
75856 KB |
Output is correct |
49 |
Correct |
109 ms |
75932 KB |
Output is correct |
50 |
Correct |
199 ms |
78276 KB |
Output is correct |
51 |
Correct |
101 ms |
65732 KB |
Output is correct |
52 |
Correct |
236 ms |
83908 KB |
Output is correct |
53 |
Correct |
122 ms |
76744 KB |
Output is correct |
54 |
Correct |
202 ms |
99676 KB |
Output is correct |
55 |
Correct |
186 ms |
84968 KB |
Output is correct |
56 |
Correct |
157 ms |
90448 KB |
Output is correct |
57 |
Correct |
174 ms |
93392 KB |
Output is correct |
58 |
Correct |
171 ms |
90960 KB |
Output is correct |
59 |
Correct |
168 ms |
90968 KB |
Output is correct |
60 |
Correct |
13 ms |
28504 KB |
Output is correct |
61 |
Correct |
12 ms |
28508 KB |
Output is correct |
62 |
Correct |
15 ms |
28508 KB |
Output is correct |
63 |
Correct |
12 ms |
28504 KB |
Output is correct |
64 |
Correct |
294 ms |
95792 KB |
Output is correct |
65 |
Correct |
217 ms |
79428 KB |
Output is correct |
66 |
Correct |
270 ms |
95712 KB |
Output is correct |
67 |
Correct |
228 ms |
79188 KB |
Output is correct |
68 |
Correct |
171 ms |
65876 KB |
Output is correct |
69 |
Correct |
160 ms |
62936 KB |
Output is correct |
70 |
Correct |
310 ms |
80024 KB |
Output is correct |
71 |
Correct |
162 ms |
69960 KB |
Output is correct |
72 |
Correct |
285 ms |
90028 KB |
Output is correct |
73 |
Correct |
165 ms |
77432 KB |
Output is correct |
74 |
Correct |
301 ms |
84388 KB |
Output is correct |
75 |
Correct |
174 ms |
72028 KB |
Output is correct |
76 |
Correct |
155 ms |
72012 KB |
Output is correct |
77 |
Correct |
241 ms |
85420 KB |
Output is correct |
78 |
Correct |
138 ms |
72772 KB |
Output is correct |
79 |
Correct |
292 ms |
100812 KB |
Output is correct |
80 |
Correct |
196 ms |
83656 KB |
Output is correct |
81 |
Correct |
157 ms |
73172 KB |
Output is correct |
82 |
Correct |
193 ms |
90348 KB |
Output is correct |
83 |
Correct |
92 ms |
66388 KB |
Output is correct |
84 |
Correct |
244 ms |
82368 KB |
Output is correct |
85 |
Correct |
266 ms |
82624 KB |
Output is correct |
86 |
Correct |
258 ms |
79300 KB |
Output is correct |
87 |
Correct |
245 ms |
82368 KB |
Output is correct |
88 |
Correct |
249 ms |
82364 KB |
Output is correct |