#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN=2e5+6;
int n;
vector<int>g[MAXN];
ll c[MAXN];
int a[MAXN],h[MAXN],subtree[MAXN];
void dfs1(int u,int p)
{
subtree[u]=1;
for(auto xd:g[u])
{
if(xd==p)continue;
dfs1(xd,u);
subtree[u]+=subtree[xd];
}
}
void dfs(int u,int p,map<int,ll> &mp)
{
int bc=-1,maxst=-1;
for(auto xd:g[u])
{
if(xd==p)continue;
if(subtree[xd]>maxst)
{
maxst=subtree[xd];
bc=xd;
}
}
if(bc==-1)
{
mp[h[u]+1]=c[u];
return;
}
dfs(bc,u,mp);
for(auto xd:g[u])
{
if(xd==p||xd==bc)continue;
map<int,ll> tmp;
dfs(xd,u,tmp);
for(auto it:tmp)
{
if(it.second!=0)mp[it.first]+=it.second;
}
}
mp[h[u]+1]+=c[u];
mp[0]+=c[u];
mp[h[u]]-=c[u];
auto it=mp.lower_bound(h[u]);
while(mp[h[u]]<0)
{
it--;
int t=(*it).first;
if(mp[t]+mp[h[u]]>=0)
{
mp[t]+=mp[h[u]];
mp[h[u]]=0;
}
else
{
mp[h[u]]+=mp[t];
mp[t]=0;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>a[i]>>h[i]>>c[i];
if(i!=1)g[a[i]].push_back(i);
}
dfs1(1,0);
map<int,ll>mp;
dfs(1,0,mp);
cout<<mp[0]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
10 ms |
5484 KB |
Output is correct |
6 |
Correct |
9 ms |
5228 KB |
Output is correct |
7 |
Correct |
8 ms |
5228 KB |
Output is correct |
8 |
Correct |
10 ms |
5484 KB |
Output is correct |
9 |
Correct |
10 ms |
5356 KB |
Output is correct |
10 |
Correct |
8 ms |
5228 KB |
Output is correct |
11 |
Correct |
7 ms |
5228 KB |
Output is correct |
12 |
Correct |
130 ms |
6960 KB |
Output is correct |
13 |
Correct |
8 ms |
6380 KB |
Output is correct |
14 |
Correct |
94 ms |
6380 KB |
Output is correct |
15 |
Correct |
15 ms |
5868 KB |
Output is correct |
16 |
Correct |
11 ms |
5612 KB |
Output is correct |
17 |
Correct |
9 ms |
5228 KB |
Output is correct |
18 |
Correct |
6 ms |
5228 KB |
Output is correct |
19 |
Correct |
14 ms |
6252 KB |
Output is correct |
20 |
Correct |
9 ms |
5740 KB |
Output is correct |
21 |
Correct |
7 ms |
5740 KB |
Output is correct |
22 |
Correct |
7 ms |
5484 KB |
Output is correct |
23 |
Correct |
6 ms |
5228 KB |
Output is correct |
24 |
Correct |
13 ms |
6252 KB |
Output is correct |
25 |
Correct |
8 ms |
5740 KB |
Output is correct |
26 |
Correct |
11 ms |
6892 KB |
Output is correct |
27 |
Correct |
161 ms |
6160 KB |
Output is correct |
28 |
Correct |
303 ms |
6380 KB |
Output is correct |
29 |
Correct |
273 ms |
6636 KB |
Output is correct |
30 |
Correct |
13 ms |
6636 KB |
Output is correct |
31 |
Correct |
13 ms |
6572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
5484 KB |
Output is correct |
2 |
Correct |
399 ms |
24556 KB |
Output is correct |
3 |
Correct |
290 ms |
12652 KB |
Output is correct |
4 |
Correct |
402 ms |
22676 KB |
Output is correct |
5 |
Correct |
314 ms |
12556 KB |
Output is correct |
6 |
Correct |
225 ms |
12268 KB |
Output is correct |
7 |
Correct |
186 ms |
12396 KB |
Output is correct |
8 |
Execution timed out |
2077 ms |
60112 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
10 ms |
5484 KB |
Output is correct |
6 |
Correct |
9 ms |
5228 KB |
Output is correct |
7 |
Correct |
8 ms |
5228 KB |
Output is correct |
8 |
Correct |
10 ms |
5484 KB |
Output is correct |
9 |
Correct |
10 ms |
5356 KB |
Output is correct |
10 |
Correct |
8 ms |
5228 KB |
Output is correct |
11 |
Correct |
7 ms |
5228 KB |
Output is correct |
12 |
Correct |
130 ms |
6960 KB |
Output is correct |
13 |
Correct |
8 ms |
6380 KB |
Output is correct |
14 |
Correct |
94 ms |
6380 KB |
Output is correct |
15 |
Correct |
15 ms |
5868 KB |
Output is correct |
16 |
Correct |
11 ms |
5612 KB |
Output is correct |
17 |
Correct |
9 ms |
5228 KB |
Output is correct |
18 |
Correct |
6 ms |
5228 KB |
Output is correct |
19 |
Correct |
14 ms |
6252 KB |
Output is correct |
20 |
Correct |
9 ms |
5740 KB |
Output is correct |
21 |
Correct |
7 ms |
5740 KB |
Output is correct |
22 |
Correct |
7 ms |
5484 KB |
Output is correct |
23 |
Correct |
6 ms |
5228 KB |
Output is correct |
24 |
Correct |
13 ms |
6252 KB |
Output is correct |
25 |
Correct |
8 ms |
5740 KB |
Output is correct |
26 |
Correct |
11 ms |
6892 KB |
Output is correct |
27 |
Correct |
161 ms |
6160 KB |
Output is correct |
28 |
Correct |
303 ms |
6380 KB |
Output is correct |
29 |
Correct |
273 ms |
6636 KB |
Output is correct |
30 |
Correct |
13 ms |
6636 KB |
Output is correct |
31 |
Correct |
13 ms |
6572 KB |
Output is correct |
32 |
Correct |
13 ms |
5484 KB |
Output is correct |
33 |
Correct |
399 ms |
24556 KB |
Output is correct |
34 |
Correct |
290 ms |
12652 KB |
Output is correct |
35 |
Correct |
402 ms |
22676 KB |
Output is correct |
36 |
Correct |
314 ms |
12556 KB |
Output is correct |
37 |
Correct |
225 ms |
12268 KB |
Output is correct |
38 |
Correct |
186 ms |
12396 KB |
Output is correct |
39 |
Execution timed out |
2077 ms |
60112 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |