#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];
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 (u==i) continue;
if (mp[u].size()>mp[i].size()) swap(mp[u], mp[i]);
for (auto [x, y]:mp[u]) mp[i][x]+=y;
}
//for (auto x:mp[i]) cout<<x.first<<' '<<x.second<<'\n';
ll cur=mp[i].begin()->second, tmp=0;
auto itr=mp[i].begin();
for (auto [x, y]:cst)
{
while (itr!=mp[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;
}
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];
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
19036 KB |
Output is correct |
2 |
Correct |
8 ms |
19036 KB |
Output is correct |
3 |
Correct |
8 ms |
19036 KB |
Output is correct |
4 |
Correct |
11 ms |
19036 KB |
Output is correct |
5 |
Correct |
13 ms |
20872 KB |
Output is correct |
6 |
Correct |
13 ms |
20316 KB |
Output is correct |
7 |
Correct |
10 ms |
20060 KB |
Output is correct |
8 |
Correct |
14 ms |
20824 KB |
Output is correct |
9 |
Correct |
12 ms |
20272 KB |
Output is correct |
10 |
Correct |
12 ms |
20060 KB |
Output is correct |
11 |
Correct |
11 ms |
20060 KB |
Output is correct |
12 |
Correct |
11 ms |
20740 KB |
Output is correct |
13 |
Correct |
10 ms |
20524 KB |
Output is correct |
14 |
Correct |
12 ms |
20312 KB |
Output is correct |
15 |
Correct |
12 ms |
20316 KB |
Output is correct |
16 |
Correct |
13 ms |
21084 KB |
Output is correct |
17 |
Correct |
11 ms |
20572 KB |
Output is correct |
18 |
Correct |
11 ms |
20060 KB |
Output is correct |
19 |
Correct |
12 ms |
20628 KB |
Output is correct |
20 |
Correct |
11 ms |
20316 KB |
Output is correct |
21 |
Correct |
10 ms |
20372 KB |
Output is correct |
22 |
Correct |
11 ms |
20532 KB |
Output is correct |
23 |
Correct |
9 ms |
20060 KB |
Output is correct |
24 |
Correct |
12 ms |
20568 KB |
Output is correct |
25 |
Correct |
11 ms |
20316 KB |
Output is correct |
26 |
Correct |
13 ms |
20828 KB |
Output is correct |
27 |
Correct |
11 ms |
20600 KB |
Output is correct |
28 |
Correct |
11 ms |
20568 KB |
Output is correct |
29 |
Correct |
11 ms |
20692 KB |
Output is correct |
30 |
Correct |
11 ms |
20832 KB |
Output is correct |
31 |
Correct |
11 ms |
20840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
19036 KB |
Output is correct |
2 |
Correct |
8 ms |
19036 KB |
Output is correct |
3 |
Correct |
8 ms |
19036 KB |
Output is correct |
4 |
Correct |
11 ms |
19036 KB |
Output is correct |
5 |
Correct |
13 ms |
20872 KB |
Output is correct |
6 |
Correct |
13 ms |
20316 KB |
Output is correct |
7 |
Correct |
10 ms |
20060 KB |
Output is correct |
8 |
Correct |
14 ms |
20824 KB |
Output is correct |
9 |
Correct |
12 ms |
20272 KB |
Output is correct |
10 |
Correct |
12 ms |
20060 KB |
Output is correct |
11 |
Correct |
11 ms |
20060 KB |
Output is correct |
12 |
Correct |
11 ms |
20740 KB |
Output is correct |
13 |
Correct |
10 ms |
20524 KB |
Output is correct |
14 |
Correct |
12 ms |
20312 KB |
Output is correct |
15 |
Correct |
12 ms |
20316 KB |
Output is correct |
16 |
Correct |
13 ms |
21084 KB |
Output is correct |
17 |
Correct |
11 ms |
20572 KB |
Output is correct |
18 |
Correct |
11 ms |
20060 KB |
Output is correct |
19 |
Correct |
12 ms |
20628 KB |
Output is correct |
20 |
Correct |
11 ms |
20316 KB |
Output is correct |
21 |
Correct |
10 ms |
20372 KB |
Output is correct |
22 |
Correct |
11 ms |
20532 KB |
Output is correct |
23 |
Correct |
9 ms |
20060 KB |
Output is correct |
24 |
Correct |
12 ms |
20568 KB |
Output is correct |
25 |
Correct |
11 ms |
20316 KB |
Output is correct |
26 |
Correct |
13 ms |
20828 KB |
Output is correct |
27 |
Correct |
11 ms |
20600 KB |
Output is correct |
28 |
Correct |
11 ms |
20568 KB |
Output is correct |
29 |
Correct |
11 ms |
20692 KB |
Output is correct |
30 |
Correct |
11 ms |
20832 KB |
Output is correct |
31 |
Correct |
11 ms |
20840 KB |
Output is correct |
32 |
Correct |
14 ms |
20840 KB |
Output is correct |
33 |
Correct |
311 ms |
101204 KB |
Output is correct |
34 |
Correct |
237 ms |
76112 KB |
Output is correct |
35 |
Correct |
295 ms |
98088 KB |
Output is correct |
36 |
Correct |
254 ms |
76184 KB |
Output is correct |
37 |
Correct |
158 ms |
57376 KB |
Output is correct |
38 |
Correct |
142 ms |
52704 KB |
Output is correct |
39 |
Correct |
156 ms |
77656 KB |
Output is correct |
40 |
Correct |
150 ms |
77396 KB |
Output is correct |
41 |
Correct |
113 ms |
77476 KB |
Output is correct |
42 |
Correct |
146 ms |
63568 KB |
Output is correct |
43 |
Correct |
144 ms |
63460 KB |
Output is correct |
44 |
Correct |
269 ms |
125520 KB |
Output is correct |
45 |
Correct |
204 ms |
90008 KB |
Output is correct |
46 |
Correct |
89 ms |
52344 KB |
Output is correct |
47 |
Correct |
208 ms |
73300 KB |
Output is correct |
48 |
Correct |
121 ms |
66388 KB |
Output is correct |
49 |
Correct |
92 ms |
66384 KB |
Output is correct |
50 |
Correct |
192 ms |
68716 KB |
Output is correct |
51 |
Correct |
99 ms |
56264 KB |
Output is correct |
52 |
Correct |
217 ms |
74432 KB |
Output is correct |
53 |
Correct |
108 ms |
67268 KB |
Output is correct |
54 |
Correct |
195 ms |
90192 KB |
Output is correct |
55 |
Correct |
172 ms |
75580 KB |
Output is correct |
56 |
Correct |
149 ms |
80960 KB |
Output is correct |
57 |
Correct |
155 ms |
83980 KB |
Output is correct |
58 |
Correct |
163 ms |
81548 KB |
Output is correct |
59 |
Correct |
163 ms |
81488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
19036 KB |
Output is correct |
2 |
Correct |
8 ms |
19036 KB |
Output is correct |
3 |
Correct |
8 ms |
19036 KB |
Output is correct |
4 |
Correct |
11 ms |
19036 KB |
Output is correct |
5 |
Correct |
13 ms |
20872 KB |
Output is correct |
6 |
Correct |
13 ms |
20316 KB |
Output is correct |
7 |
Correct |
10 ms |
20060 KB |
Output is correct |
8 |
Correct |
14 ms |
20824 KB |
Output is correct |
9 |
Correct |
12 ms |
20272 KB |
Output is correct |
10 |
Correct |
12 ms |
20060 KB |
Output is correct |
11 |
Correct |
11 ms |
20060 KB |
Output is correct |
12 |
Correct |
11 ms |
20740 KB |
Output is correct |
13 |
Correct |
10 ms |
20524 KB |
Output is correct |
14 |
Correct |
12 ms |
20312 KB |
Output is correct |
15 |
Correct |
12 ms |
20316 KB |
Output is correct |
16 |
Correct |
13 ms |
21084 KB |
Output is correct |
17 |
Correct |
11 ms |
20572 KB |
Output is correct |
18 |
Correct |
11 ms |
20060 KB |
Output is correct |
19 |
Correct |
12 ms |
20628 KB |
Output is correct |
20 |
Correct |
11 ms |
20316 KB |
Output is correct |
21 |
Correct |
10 ms |
20372 KB |
Output is correct |
22 |
Correct |
11 ms |
20532 KB |
Output is correct |
23 |
Correct |
9 ms |
20060 KB |
Output is correct |
24 |
Correct |
12 ms |
20568 KB |
Output is correct |
25 |
Correct |
11 ms |
20316 KB |
Output is correct |
26 |
Correct |
13 ms |
20828 KB |
Output is correct |
27 |
Correct |
11 ms |
20600 KB |
Output is correct |
28 |
Correct |
11 ms |
20568 KB |
Output is correct |
29 |
Correct |
11 ms |
20692 KB |
Output is correct |
30 |
Correct |
11 ms |
20832 KB |
Output is correct |
31 |
Correct |
11 ms |
20840 KB |
Output is correct |
32 |
Correct |
14 ms |
20840 KB |
Output is correct |
33 |
Correct |
311 ms |
101204 KB |
Output is correct |
34 |
Correct |
237 ms |
76112 KB |
Output is correct |
35 |
Correct |
295 ms |
98088 KB |
Output is correct |
36 |
Correct |
254 ms |
76184 KB |
Output is correct |
37 |
Correct |
158 ms |
57376 KB |
Output is correct |
38 |
Correct |
142 ms |
52704 KB |
Output is correct |
39 |
Correct |
156 ms |
77656 KB |
Output is correct |
40 |
Correct |
150 ms |
77396 KB |
Output is correct |
41 |
Correct |
113 ms |
77476 KB |
Output is correct |
42 |
Correct |
146 ms |
63568 KB |
Output is correct |
43 |
Correct |
144 ms |
63460 KB |
Output is correct |
44 |
Correct |
269 ms |
125520 KB |
Output is correct |
45 |
Correct |
204 ms |
90008 KB |
Output is correct |
46 |
Correct |
89 ms |
52344 KB |
Output is correct |
47 |
Correct |
208 ms |
73300 KB |
Output is correct |
48 |
Correct |
121 ms |
66388 KB |
Output is correct |
49 |
Correct |
92 ms |
66384 KB |
Output is correct |
50 |
Correct |
192 ms |
68716 KB |
Output is correct |
51 |
Correct |
99 ms |
56264 KB |
Output is correct |
52 |
Correct |
217 ms |
74432 KB |
Output is correct |
53 |
Correct |
108 ms |
67268 KB |
Output is correct |
54 |
Correct |
195 ms |
90192 KB |
Output is correct |
55 |
Correct |
172 ms |
75580 KB |
Output is correct |
56 |
Correct |
149 ms |
80960 KB |
Output is correct |
57 |
Correct |
155 ms |
83980 KB |
Output is correct |
58 |
Correct |
163 ms |
81548 KB |
Output is correct |
59 |
Correct |
163 ms |
81488 KB |
Output is correct |
60 |
Incorrect |
8 ms |
19032 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |