#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=2e5+5;
int N;
int A[MX],H[MX],C[MX];
vector<int> adj[MX];
multiset<pair<ll,ll>> dp[MX];
int par[MX];
int find(int v) {
return par[v]==v?v:par[v]=find(par[v]);
}
void merge(int u, int v) {
u=find(u),v=find(v);
if(u==v) return;
if(dp[u].size()>dp[v].size()) swap(u,v);
for(auto [pos,val]:dp[u]) {
dp[v].insert({pos,val});
}
par[u]=v;
dp[u].clear();
}
void dfs(int v, int p) {
int prv=-1;
for(auto u:adj[v]) {
if(u==p) continue;
dfs(u,v);
if(prv!=-1)
merge(u,prv);
prv=u;
}
if(prv!=-1) merge(v,prv);
int pv=find(v);
auto it=dp[pv].lower_bound(make_pair(H[v],0));
if(it==dp[pv].begin()) {
dp[pv].insert(make_pair(H[v],C[v]));
} else {
it--;
ll cur=C[v];
while(true) {
if(cur>=it->second) {
cur-=it->second;
it=dp[pv].erase(it);
if(it==dp[pv].begin()) break;
it--;
} else {
auto [p,q]=*it;
dp[pv].erase(it);
dp[pv].insert({p,q-cur});
cur=0;
break;
}
}
dp[pv].insert(make_pair(H[v],C[v]));
}
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
// set<int> st;
// st.insert(1);
// st.insert(2);
// st.insert(3);
// auto it=st.lower_bound(2);
// it=st.erase(it);
// cout<<(*it)<<'\n';
cin>>N;
ll sum=0;
for(int i=1;i<=N;i++) {
cin>>A[i]>>H[i]>>C[i];
sum+=C[i];
}
for(int i=2;i<=N;i++) {
adj[A[i]].push_back(i);
}
for(int i=1;i<=N;i++) par[i]=i;
dfs(1,0);
ll ans=0;
for(auto [pos,val]:dp[find(1)]) {
ans+=val;
}
cout<<sum-ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16828 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
7 ms |
17244 KB |
Output is correct |
6 |
Correct |
7 ms |
17144 KB |
Output is correct |
7 |
Correct |
7 ms |
17244 KB |
Output is correct |
8 |
Correct |
9 ms |
17244 KB |
Output is correct |
9 |
Correct |
7 ms |
17244 KB |
Output is correct |
10 |
Correct |
7 ms |
17404 KB |
Output is correct |
11 |
Correct |
7 ms |
17244 KB |
Output is correct |
12 |
Correct |
6 ms |
17756 KB |
Output is correct |
13 |
Correct |
7 ms |
17756 KB |
Output is correct |
14 |
Correct |
6 ms |
17500 KB |
Output is correct |
15 |
Correct |
8 ms |
17500 KB |
Output is correct |
16 |
Correct |
7 ms |
17360 KB |
Output is correct |
17 |
Correct |
7 ms |
17244 KB |
Output is correct |
18 |
Correct |
8 ms |
17500 KB |
Output is correct |
19 |
Correct |
7 ms |
17500 KB |
Output is correct |
20 |
Correct |
6 ms |
17500 KB |
Output is correct |
21 |
Correct |
6 ms |
17500 KB |
Output is correct |
22 |
Correct |
6 ms |
17244 KB |
Output is correct |
23 |
Correct |
6 ms |
17244 KB |
Output is correct |
24 |
Correct |
7 ms |
17500 KB |
Output is correct |
25 |
Correct |
7 ms |
17516 KB |
Output is correct |
26 |
Correct |
5 ms |
17756 KB |
Output is correct |
27 |
Correct |
6 ms |
17500 KB |
Output is correct |
28 |
Correct |
7 ms |
17500 KB |
Output is correct |
29 |
Correct |
6 ms |
17804 KB |
Output is correct |
30 |
Correct |
6 ms |
17756 KB |
Output is correct |
31 |
Correct |
6 ms |
17756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16828 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
7 ms |
17244 KB |
Output is correct |
6 |
Correct |
7 ms |
17144 KB |
Output is correct |
7 |
Correct |
7 ms |
17244 KB |
Output is correct |
8 |
Correct |
9 ms |
17244 KB |
Output is correct |
9 |
Correct |
7 ms |
17244 KB |
Output is correct |
10 |
Correct |
7 ms |
17404 KB |
Output is correct |
11 |
Correct |
7 ms |
17244 KB |
Output is correct |
12 |
Correct |
6 ms |
17756 KB |
Output is correct |
13 |
Correct |
7 ms |
17756 KB |
Output is correct |
14 |
Correct |
6 ms |
17500 KB |
Output is correct |
15 |
Correct |
8 ms |
17500 KB |
Output is correct |
16 |
Correct |
7 ms |
17360 KB |
Output is correct |
17 |
Correct |
7 ms |
17244 KB |
Output is correct |
18 |
Correct |
8 ms |
17500 KB |
Output is correct |
19 |
Correct |
7 ms |
17500 KB |
Output is correct |
20 |
Correct |
6 ms |
17500 KB |
Output is correct |
21 |
Correct |
6 ms |
17500 KB |
Output is correct |
22 |
Correct |
6 ms |
17244 KB |
Output is correct |
23 |
Correct |
6 ms |
17244 KB |
Output is correct |
24 |
Correct |
7 ms |
17500 KB |
Output is correct |
25 |
Correct |
7 ms |
17516 KB |
Output is correct |
26 |
Correct |
5 ms |
17756 KB |
Output is correct |
27 |
Correct |
6 ms |
17500 KB |
Output is correct |
28 |
Correct |
7 ms |
17500 KB |
Output is correct |
29 |
Correct |
6 ms |
17804 KB |
Output is correct |
30 |
Correct |
6 ms |
17756 KB |
Output is correct |
31 |
Correct |
6 ms |
17756 KB |
Output is correct |
32 |
Correct |
8 ms |
17244 KB |
Output is correct |
33 |
Correct |
227 ms |
38280 KB |
Output is correct |
34 |
Correct |
217 ms |
36948 KB |
Output is correct |
35 |
Correct |
203 ms |
36064 KB |
Output is correct |
36 |
Correct |
225 ms |
37620 KB |
Output is correct |
37 |
Correct |
228 ms |
39504 KB |
Output is correct |
38 |
Correct |
284 ms |
38320 KB |
Output is correct |
39 |
Correct |
129 ms |
54040 KB |
Output is correct |
40 |
Correct |
134 ms |
54100 KB |
Output is correct |
41 |
Correct |
134 ms |
57376 KB |
Output is correct |
42 |
Correct |
123 ms |
41552 KB |
Output is correct |
43 |
Correct |
125 ms |
41812 KB |
Output is correct |
44 |
Correct |
216 ms |
38036 KB |
Output is correct |
45 |
Correct |
210 ms |
37964 KB |
Output is correct |
46 |
Correct |
254 ms |
42460 KB |
Output is correct |
47 |
Correct |
162 ms |
45212 KB |
Output is correct |
48 |
Correct |
187 ms |
49032 KB |
Output is correct |
49 |
Correct |
179 ms |
49492 KB |
Output is correct |
50 |
Correct |
150 ms |
35020 KB |
Output is correct |
51 |
Correct |
158 ms |
35156 KB |
Output is correct |
52 |
Correct |
193 ms |
45768 KB |
Output is correct |
53 |
Correct |
201 ms |
49864 KB |
Output is correct |
54 |
Correct |
76 ms |
54096 KB |
Output is correct |
55 |
Correct |
123 ms |
46928 KB |
Output is correct |
56 |
Correct |
117 ms |
55120 KB |
Output is correct |
57 |
Correct |
124 ms |
59020 KB |
Output is correct |
58 |
Correct |
186 ms |
59428 KB |
Output is correct |
59 |
Correct |
191 ms |
59572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16828 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
7 ms |
17244 KB |
Output is correct |
6 |
Correct |
7 ms |
17144 KB |
Output is correct |
7 |
Correct |
7 ms |
17244 KB |
Output is correct |
8 |
Correct |
9 ms |
17244 KB |
Output is correct |
9 |
Correct |
7 ms |
17244 KB |
Output is correct |
10 |
Correct |
7 ms |
17404 KB |
Output is correct |
11 |
Correct |
7 ms |
17244 KB |
Output is correct |
12 |
Correct |
6 ms |
17756 KB |
Output is correct |
13 |
Correct |
7 ms |
17756 KB |
Output is correct |
14 |
Correct |
6 ms |
17500 KB |
Output is correct |
15 |
Correct |
8 ms |
17500 KB |
Output is correct |
16 |
Correct |
7 ms |
17360 KB |
Output is correct |
17 |
Correct |
7 ms |
17244 KB |
Output is correct |
18 |
Correct |
8 ms |
17500 KB |
Output is correct |
19 |
Correct |
7 ms |
17500 KB |
Output is correct |
20 |
Correct |
6 ms |
17500 KB |
Output is correct |
21 |
Correct |
6 ms |
17500 KB |
Output is correct |
22 |
Correct |
6 ms |
17244 KB |
Output is correct |
23 |
Correct |
6 ms |
17244 KB |
Output is correct |
24 |
Correct |
7 ms |
17500 KB |
Output is correct |
25 |
Correct |
7 ms |
17516 KB |
Output is correct |
26 |
Correct |
5 ms |
17756 KB |
Output is correct |
27 |
Correct |
6 ms |
17500 KB |
Output is correct |
28 |
Correct |
7 ms |
17500 KB |
Output is correct |
29 |
Correct |
6 ms |
17804 KB |
Output is correct |
30 |
Correct |
6 ms |
17756 KB |
Output is correct |
31 |
Correct |
6 ms |
17756 KB |
Output is correct |
32 |
Correct |
8 ms |
17244 KB |
Output is correct |
33 |
Correct |
227 ms |
38280 KB |
Output is correct |
34 |
Correct |
217 ms |
36948 KB |
Output is correct |
35 |
Correct |
203 ms |
36064 KB |
Output is correct |
36 |
Correct |
225 ms |
37620 KB |
Output is correct |
37 |
Correct |
228 ms |
39504 KB |
Output is correct |
38 |
Correct |
284 ms |
38320 KB |
Output is correct |
39 |
Correct |
129 ms |
54040 KB |
Output is correct |
40 |
Correct |
134 ms |
54100 KB |
Output is correct |
41 |
Correct |
134 ms |
57376 KB |
Output is correct |
42 |
Correct |
123 ms |
41552 KB |
Output is correct |
43 |
Correct |
125 ms |
41812 KB |
Output is correct |
44 |
Correct |
216 ms |
38036 KB |
Output is correct |
45 |
Correct |
210 ms |
37964 KB |
Output is correct |
46 |
Correct |
254 ms |
42460 KB |
Output is correct |
47 |
Correct |
162 ms |
45212 KB |
Output is correct |
48 |
Correct |
187 ms |
49032 KB |
Output is correct |
49 |
Correct |
179 ms |
49492 KB |
Output is correct |
50 |
Correct |
150 ms |
35020 KB |
Output is correct |
51 |
Correct |
158 ms |
35156 KB |
Output is correct |
52 |
Correct |
193 ms |
45768 KB |
Output is correct |
53 |
Correct |
201 ms |
49864 KB |
Output is correct |
54 |
Correct |
76 ms |
54096 KB |
Output is correct |
55 |
Correct |
123 ms |
46928 KB |
Output is correct |
56 |
Correct |
117 ms |
55120 KB |
Output is correct |
57 |
Correct |
124 ms |
59020 KB |
Output is correct |
58 |
Correct |
186 ms |
59428 KB |
Output is correct |
59 |
Correct |
191 ms |
59572 KB |
Output is correct |
60 |
Correct |
3 ms |
16728 KB |
Output is correct |
61 |
Incorrect |
3 ms |
16876 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |