#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define pp pair<ll,int>
#define ub(x,i) upper_bound(all(x),i)-x.begin()
using namespace std;
const int mxn=2e5+5;
vector<int> g[mxn];
int a[mxn],h[mxn],c[mxn];
map<int,ll>dp[mxn];
bool vis[mxn]{0};
void dfs(int u){
vis[u]=1;
for(auto v:g[u]){
if(vis[v])continue;
dfs(v);
if(dp[v].size()>dp[u].size())swap(dp[u],dp[v]);
for(auto it:dp[v])dp[u][it.f]+=it.s;
}dp[u][h[u]]+=c[u];
auto it = dp[u].upper_bound(h[u]);
int cur=c[u];
for(;it!=dp[u].end();){
if(cur>=it->s){cur-=it->s;auto ij=it;it++;dp[u].erase(ij);}
else {it->s-=cur;break;}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;ll rs=0;
for(int i=1;i<=n;i++){
cin>>a[i]>>h[i]>>c[i];h[i]=-h[i];
g[a[i]].pb(i);rs+=c[i];
}dfs(1);ll rs2=0;
for(auto it:dp[1])rs2+=it.s;
cout<<rs-rs2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
3 ms |
16720 KB |
Output is correct |
3 |
Correct |
3 ms |
16720 KB |
Output is correct |
4 |
Correct |
3 ms |
16720 KB |
Output is correct |
5 |
Correct |
6 ms |
17744 KB |
Output is correct |
6 |
Correct |
5 ms |
17488 KB |
Output is correct |
7 |
Correct |
5 ms |
17088 KB |
Output is correct |
8 |
Correct |
6 ms |
18000 KB |
Output is correct |
9 |
Correct |
5 ms |
17488 KB |
Output is correct |
10 |
Correct |
4 ms |
17232 KB |
Output is correct |
11 |
Correct |
4 ms |
16976 KB |
Output is correct |
12 |
Correct |
5 ms |
17804 KB |
Output is correct |
13 |
Correct |
4 ms |
17744 KB |
Output is correct |
14 |
Correct |
5 ms |
17232 KB |
Output is correct |
15 |
Correct |
5 ms |
17232 KB |
Output is correct |
16 |
Correct |
7 ms |
18256 KB |
Output is correct |
17 |
Correct |
6 ms |
17744 KB |
Output is correct |
18 |
Correct |
4 ms |
16976 KB |
Output is correct |
19 |
Correct |
5 ms |
17488 KB |
Output is correct |
20 |
Correct |
4 ms |
17488 KB |
Output is correct |
21 |
Correct |
4 ms |
17488 KB |
Output is correct |
22 |
Correct |
5 ms |
17488 KB |
Output is correct |
23 |
Correct |
5 ms |
17232 KB |
Output is correct |
24 |
Correct |
5 ms |
17656 KB |
Output is correct |
25 |
Correct |
4 ms |
17488 KB |
Output is correct |
26 |
Correct |
5 ms |
17744 KB |
Output is correct |
27 |
Correct |
5 ms |
17488 KB |
Output is correct |
28 |
Correct |
5 ms |
17744 KB |
Output is correct |
29 |
Correct |
5 ms |
17744 KB |
Output is correct |
30 |
Correct |
5 ms |
17744 KB |
Output is correct |
31 |
Correct |
5 ms |
17916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
3 ms |
16720 KB |
Output is correct |
3 |
Correct |
3 ms |
16720 KB |
Output is correct |
4 |
Correct |
3 ms |
16720 KB |
Output is correct |
5 |
Correct |
6 ms |
17744 KB |
Output is correct |
6 |
Correct |
5 ms |
17488 KB |
Output is correct |
7 |
Correct |
5 ms |
17088 KB |
Output is correct |
8 |
Correct |
6 ms |
18000 KB |
Output is correct |
9 |
Correct |
5 ms |
17488 KB |
Output is correct |
10 |
Correct |
4 ms |
17232 KB |
Output is correct |
11 |
Correct |
4 ms |
16976 KB |
Output is correct |
12 |
Correct |
5 ms |
17804 KB |
Output is correct |
13 |
Correct |
4 ms |
17744 KB |
Output is correct |
14 |
Correct |
5 ms |
17232 KB |
Output is correct |
15 |
Correct |
5 ms |
17232 KB |
Output is correct |
16 |
Correct |
7 ms |
18256 KB |
Output is correct |
17 |
Correct |
6 ms |
17744 KB |
Output is correct |
18 |
Correct |
4 ms |
16976 KB |
Output is correct |
19 |
Correct |
5 ms |
17488 KB |
Output is correct |
20 |
Correct |
4 ms |
17488 KB |
Output is correct |
21 |
Correct |
4 ms |
17488 KB |
Output is correct |
22 |
Correct |
5 ms |
17488 KB |
Output is correct |
23 |
Correct |
5 ms |
17232 KB |
Output is correct |
24 |
Correct |
5 ms |
17656 KB |
Output is correct |
25 |
Correct |
4 ms |
17488 KB |
Output is correct |
26 |
Correct |
5 ms |
17744 KB |
Output is correct |
27 |
Correct |
5 ms |
17488 KB |
Output is correct |
28 |
Correct |
5 ms |
17744 KB |
Output is correct |
29 |
Correct |
5 ms |
17744 KB |
Output is correct |
30 |
Correct |
5 ms |
17744 KB |
Output is correct |
31 |
Correct |
5 ms |
17916 KB |
Output is correct |
32 |
Correct |
6 ms |
17744 KB |
Output is correct |
33 |
Correct |
223 ms |
74756 KB |
Output is correct |
34 |
Correct |
165 ms |
49736 KB |
Output is correct |
35 |
Correct |
230 ms |
71748 KB |
Output is correct |
36 |
Correct |
184 ms |
49716 KB |
Output is correct |
37 |
Correct |
84 ms |
30792 KB |
Output is correct |
38 |
Correct |
70 ms |
26440 KB |
Output is correct |
39 |
Correct |
126 ms |
51528 KB |
Output is correct |
40 |
Correct |
101 ms |
51336 KB |
Output is correct |
41 |
Correct |
79 ms |
51272 KB |
Output is correct |
42 |
Correct |
92 ms |
37448 KB |
Output is correct |
43 |
Correct |
87 ms |
37392 KB |
Output is correct |
44 |
Correct |
250 ms |
99660 KB |
Output is correct |
45 |
Correct |
169 ms |
64072 KB |
Output is correct |
46 |
Correct |
54 ms |
26452 KB |
Output is correct |
47 |
Correct |
151 ms |
47376 KB |
Output is correct |
48 |
Correct |
79 ms |
40520 KB |
Output is correct |
49 |
Correct |
67 ms |
40464 KB |
Output is correct |
50 |
Correct |
168 ms |
42692 KB |
Output is correct |
51 |
Correct |
65 ms |
30404 KB |
Output is correct |
52 |
Correct |
176 ms |
48064 KB |
Output is correct |
53 |
Correct |
92 ms |
40896 KB |
Output is correct |
54 |
Correct |
82 ms |
51528 KB |
Output is correct |
55 |
Correct |
114 ms |
49792 KB |
Output is correct |
56 |
Correct |
117 ms |
55896 KB |
Output is correct |
57 |
Correct |
106 ms |
58696 KB |
Output is correct |
58 |
Correct |
127 ms |
55980 KB |
Output is correct |
59 |
Correct |
117 ms |
56036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
3 ms |
16720 KB |
Output is correct |
3 |
Correct |
3 ms |
16720 KB |
Output is correct |
4 |
Correct |
3 ms |
16720 KB |
Output is correct |
5 |
Correct |
6 ms |
17744 KB |
Output is correct |
6 |
Correct |
5 ms |
17488 KB |
Output is correct |
7 |
Correct |
5 ms |
17088 KB |
Output is correct |
8 |
Correct |
6 ms |
18000 KB |
Output is correct |
9 |
Correct |
5 ms |
17488 KB |
Output is correct |
10 |
Correct |
4 ms |
17232 KB |
Output is correct |
11 |
Correct |
4 ms |
16976 KB |
Output is correct |
12 |
Correct |
5 ms |
17804 KB |
Output is correct |
13 |
Correct |
4 ms |
17744 KB |
Output is correct |
14 |
Correct |
5 ms |
17232 KB |
Output is correct |
15 |
Correct |
5 ms |
17232 KB |
Output is correct |
16 |
Correct |
7 ms |
18256 KB |
Output is correct |
17 |
Correct |
6 ms |
17744 KB |
Output is correct |
18 |
Correct |
4 ms |
16976 KB |
Output is correct |
19 |
Correct |
5 ms |
17488 KB |
Output is correct |
20 |
Correct |
4 ms |
17488 KB |
Output is correct |
21 |
Correct |
4 ms |
17488 KB |
Output is correct |
22 |
Correct |
5 ms |
17488 KB |
Output is correct |
23 |
Correct |
5 ms |
17232 KB |
Output is correct |
24 |
Correct |
5 ms |
17656 KB |
Output is correct |
25 |
Correct |
4 ms |
17488 KB |
Output is correct |
26 |
Correct |
5 ms |
17744 KB |
Output is correct |
27 |
Correct |
5 ms |
17488 KB |
Output is correct |
28 |
Correct |
5 ms |
17744 KB |
Output is correct |
29 |
Correct |
5 ms |
17744 KB |
Output is correct |
30 |
Correct |
5 ms |
17744 KB |
Output is correct |
31 |
Correct |
5 ms |
17916 KB |
Output is correct |
32 |
Correct |
6 ms |
17744 KB |
Output is correct |
33 |
Correct |
223 ms |
74756 KB |
Output is correct |
34 |
Correct |
165 ms |
49736 KB |
Output is correct |
35 |
Correct |
230 ms |
71748 KB |
Output is correct |
36 |
Correct |
184 ms |
49716 KB |
Output is correct |
37 |
Correct |
84 ms |
30792 KB |
Output is correct |
38 |
Correct |
70 ms |
26440 KB |
Output is correct |
39 |
Correct |
126 ms |
51528 KB |
Output is correct |
40 |
Correct |
101 ms |
51336 KB |
Output is correct |
41 |
Correct |
79 ms |
51272 KB |
Output is correct |
42 |
Correct |
92 ms |
37448 KB |
Output is correct |
43 |
Correct |
87 ms |
37392 KB |
Output is correct |
44 |
Correct |
250 ms |
99660 KB |
Output is correct |
45 |
Correct |
169 ms |
64072 KB |
Output is correct |
46 |
Correct |
54 ms |
26452 KB |
Output is correct |
47 |
Correct |
151 ms |
47376 KB |
Output is correct |
48 |
Correct |
79 ms |
40520 KB |
Output is correct |
49 |
Correct |
67 ms |
40464 KB |
Output is correct |
50 |
Correct |
168 ms |
42692 KB |
Output is correct |
51 |
Correct |
65 ms |
30404 KB |
Output is correct |
52 |
Correct |
176 ms |
48064 KB |
Output is correct |
53 |
Correct |
92 ms |
40896 KB |
Output is correct |
54 |
Correct |
82 ms |
51528 KB |
Output is correct |
55 |
Correct |
114 ms |
49792 KB |
Output is correct |
56 |
Correct |
117 ms |
55896 KB |
Output is correct |
57 |
Correct |
106 ms |
58696 KB |
Output is correct |
58 |
Correct |
127 ms |
55980 KB |
Output is correct |
59 |
Correct |
117 ms |
56036 KB |
Output is correct |
60 |
Correct |
3 ms |
16720 KB |
Output is correct |
61 |
Incorrect |
3 ms |
16720 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |