#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll,ll>;
const ll maxn=1e5+10;
ll p[maxn];
vector<ll> g[maxn];
pair<ll,ll> f[maxn];
set<pair<ll,ll> > dp[maxn]; // obrisao sam ceo subtree pre first-og dana i zaradio second vise nego za prosli koji imam sacuvan
void dodajsub(pair<ll,ll> f,set<pii> &dp){
auto it=dp.lower_bound({f.first,-1});
if(it==dp.end()){
dp.emplace(f);
return;
}
if(it->first!=f.first){
dp.emplace(f);
return;
}
pii nov=*it;
nov.second+=f.second;
dp.erase(it);
dp.emplace(nov);
}
void dodajcale(pii f,set<pii> &dp){
auto it=dp.lower_bound({f.first,-1});
if(it==dp.end()){
dp.emplace(f);
return;
}
pii nov=f;
if(it->first==f.first){
nov.second+=it->second;
dp.erase(it);
}
it=dp.lower_bound({f.first,-1});
ll tren=0;
while(it!=dp.end() && tren+it->second<=f.second){
tren+=it->second;
dp.erase(it);
it=dp.lower_bound({f.first,-1});
}
if(it==dp.end()){
dp.emplace(nov);
return;
}
pii sled=*it;
dp.erase(it);
dp.emplace(nov);
sled.second-=f.second-tren;
dp.emplace(sled);
}
void dfs(ll node=1){
for(ll i:g[node]) dfs(i);
pair<ll,ll> maxi={-1,-1};
for(ll i:g[node]) maxi=max(maxi,pii(dp[i].size(),i));
if(maxi.second!=-1){
swap(dp[node],dp[maxi.second]);
for(ll i:g[node]){
if(i==maxi.second) continue;
for(auto j:dp[i]){
dodajsub(j,dp[node]);
}
}
}
if(f[node].first!=-1) dodajcale(f[node],dp[node]);
/*cout<<node<<":\n";
for(auto i:dp[node]) cout<<i.first<<" "<<i.second<<"\n";
cout<<"\n";*/
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n,m,k;
cin>>n>>m>>k;
for(ll i=2;i<=n;i++){
cin>>p[i];
g[p[i]].push_back(i);
}
for(ll i=1;i<=n;i++) f[i]={-1,-1};
for(ll i=0;i<m;i++){
ll v,d,w; cin>>v>>d>>w;
f[v]={d,w};
}
dfs();
ll res=0;
for(auto i:dp[1]) res+=i.second;
cout<<res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
19148 KB |
Output is correct |
2 |
Correct |
34 ms |
18296 KB |
Output is correct |
3 |
Correct |
145 ms |
35276 KB |
Output is correct |
4 |
Correct |
91 ms |
19848 KB |
Output is correct |
5 |
Correct |
135 ms |
21052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
5 ms |
7508 KB |
Output is correct |
3 |
Correct |
4 ms |
7508 KB |
Output is correct |
4 |
Correct |
50 ms |
20560 KB |
Output is correct |
5 |
Correct |
84 ms |
24612 KB |
Output is correct |
6 |
Correct |
87 ms |
20636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
15136 KB |
Output is correct |
2 |
Correct |
48 ms |
14392 KB |
Output is correct |
3 |
Correct |
52 ms |
16416 KB |
Output is correct |
4 |
Correct |
35 ms |
16104 KB |
Output is correct |
5 |
Correct |
45 ms |
20644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
85 ms |
17960 KB |
Output is correct |
11 |
Correct |
87 ms |
16964 KB |
Output is correct |
12 |
Correct |
60 ms |
16504 KB |
Output is correct |
13 |
Correct |
37 ms |
16016 KB |
Output is correct |
14 |
Correct |
47 ms |
20624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8404 KB |
Output is correct |
2 |
Correct |
64 ms |
11676 KB |
Output is correct |
3 |
Correct |
39 ms |
11628 KB |
Output is correct |
4 |
Correct |
38 ms |
11772 KB |
Output is correct |
5 |
Correct |
10 ms |
10576 KB |
Output is correct |
6 |
Correct |
24 ms |
12944 KB |
Output is correct |
7 |
Correct |
21 ms |
15440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
5 ms |
7508 KB |
Output is correct |
12 |
Correct |
4 ms |
7508 KB |
Output is correct |
13 |
Correct |
50 ms |
20560 KB |
Output is correct |
14 |
Correct |
84 ms |
24612 KB |
Output is correct |
15 |
Correct |
87 ms |
20636 KB |
Output is correct |
16 |
Correct |
85 ms |
17960 KB |
Output is correct |
17 |
Correct |
87 ms |
16964 KB |
Output is correct |
18 |
Correct |
60 ms |
16504 KB |
Output is correct |
19 |
Correct |
37 ms |
16016 KB |
Output is correct |
20 |
Correct |
47 ms |
20624 KB |
Output is correct |
21 |
Correct |
17 ms |
10628 KB |
Output is correct |
22 |
Correct |
80 ms |
19212 KB |
Output is correct |
23 |
Correct |
95 ms |
22464 KB |
Output is correct |
24 |
Correct |
132 ms |
29952 KB |
Output is correct |
25 |
Correct |
76 ms |
19784 KB |
Output is correct |
26 |
Correct |
134 ms |
20300 KB |
Output is correct |
27 |
Correct |
94 ms |
17960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7252 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
56 ms |
19148 KB |
Output is correct |
11 |
Correct |
34 ms |
18296 KB |
Output is correct |
12 |
Correct |
145 ms |
35276 KB |
Output is correct |
13 |
Correct |
91 ms |
19848 KB |
Output is correct |
14 |
Correct |
135 ms |
21052 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
5 ms |
7508 KB |
Output is correct |
17 |
Correct |
4 ms |
7508 KB |
Output is correct |
18 |
Correct |
50 ms |
20560 KB |
Output is correct |
19 |
Correct |
84 ms |
24612 KB |
Output is correct |
20 |
Correct |
87 ms |
20636 KB |
Output is correct |
21 |
Correct |
53 ms |
15136 KB |
Output is correct |
22 |
Correct |
48 ms |
14392 KB |
Output is correct |
23 |
Correct |
52 ms |
16416 KB |
Output is correct |
24 |
Correct |
35 ms |
16104 KB |
Output is correct |
25 |
Correct |
45 ms |
20644 KB |
Output is correct |
26 |
Correct |
85 ms |
17960 KB |
Output is correct |
27 |
Correct |
87 ms |
16964 KB |
Output is correct |
28 |
Correct |
60 ms |
16504 KB |
Output is correct |
29 |
Correct |
37 ms |
16016 KB |
Output is correct |
30 |
Correct |
47 ms |
20624 KB |
Output is correct |
31 |
Correct |
8 ms |
8404 KB |
Output is correct |
32 |
Correct |
64 ms |
11676 KB |
Output is correct |
33 |
Correct |
39 ms |
11628 KB |
Output is correct |
34 |
Correct |
38 ms |
11772 KB |
Output is correct |
35 |
Correct |
10 ms |
10576 KB |
Output is correct |
36 |
Correct |
24 ms |
12944 KB |
Output is correct |
37 |
Correct |
21 ms |
15440 KB |
Output is correct |
38 |
Correct |
17 ms |
10628 KB |
Output is correct |
39 |
Correct |
80 ms |
19212 KB |
Output is correct |
40 |
Correct |
95 ms |
22464 KB |
Output is correct |
41 |
Correct |
132 ms |
29952 KB |
Output is correct |
42 |
Correct |
76 ms |
19784 KB |
Output is correct |
43 |
Correct |
134 ms |
20300 KB |
Output is correct |
44 |
Correct |
94 ms |
17960 KB |
Output is correct |
45 |
Correct |
38 ms |
11032 KB |
Output is correct |
46 |
Correct |
77 ms |
19916 KB |
Output is correct |
47 |
Correct |
78 ms |
23272 KB |
Output is correct |
48 |
Correct |
144 ms |
32336 KB |
Output is correct |
49 |
Correct |
98 ms |
19852 KB |
Output is correct |
50 |
Correct |
129 ms |
20592 KB |
Output is correct |
51 |
Correct |
114 ms |
18156 KB |
Output is correct |