#include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e9;
int n,m,k;
vector<int> con[100005];
int parent[100005];
int d[100005],w[100005];
map<int,int> dp[100005];
void dfs(int nod)
{
int maxc=-1,heavy=-1;
for(int adj:con[nod])
{
dfs(adj);
if((int)dp[adj].size() > maxc)
maxc = dp[adj].size(), heavy = adj;
}
if(heavy!=-1) swap(dp[nod], dp[heavy]);
for(int adj:con[nod])
{
if(adj==heavy)
continue;
for(auto it:dp[adj])
dp[nod][it.first] += it.second;
}
if(w[nod] > 0)
{
dp[nod][d[nod]] += w[nod];
auto it = dp[nod].upper_bound(d[nod]);
int cur = w[nod];
while(it!=dp[nod].end())
{
if((*it).second < cur)
{
cur -= (*it).second;
it = dp[nod].erase(it);
}
else
{
pair<int,int> cop = *it;
dp[nod][cop.first] -= cur;
cur = 0;
break;
}
}
}
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m>>k;
for(int i=2;i<=n;i++)
{
cin>>parent[i];
con[parent[i]].push_back(i);
}
for(int i=1;i<=m;i++)
{
int x;
cin>>x;
cin>>d[x]>>w[x];
}
dfs(1);
int sum=0;
for(auto it:dp[1])
sum += it.second;
cout<<sum;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
9808 KB |
Output is correct |
2 |
Correct |
2 ms |
9808 KB |
Output is correct |
3 |
Correct |
3 ms |
9976 KB |
Output is correct |
4 |
Correct |
5 ms |
8016 KB |
Output is correct |
5 |
Correct |
5 ms |
8032 KB |
Output is correct |
6 |
Correct |
5 ms |
8024 KB |
Output is correct |
7 |
Correct |
6 ms |
8024 KB |
Output is correct |
8 |
Correct |
6 ms |
8244 KB |
Output is correct |
9 |
Correct |
5 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
19024 KB |
Output is correct |
2 |
Correct |
51 ms |
19832 KB |
Output is correct |
3 |
Correct |
129 ms |
35460 KB |
Output is correct |
4 |
Correct |
73 ms |
19920 KB |
Output is correct |
5 |
Correct |
95 ms |
21532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8284 KB |
Output is correct |
2 |
Correct |
6 ms |
8288 KB |
Output is correct |
3 |
Correct |
5 ms |
8284 KB |
Output is correct |
4 |
Correct |
47 ms |
26972 KB |
Output is correct |
5 |
Correct |
81 ms |
30808 KB |
Output is correct |
6 |
Correct |
71 ms |
26964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
15188 KB |
Output is correct |
2 |
Correct |
51 ms |
14420 KB |
Output is correct |
3 |
Correct |
45 ms |
19028 KB |
Output is correct |
4 |
Correct |
39 ms |
16076 KB |
Output is correct |
5 |
Correct |
45 ms |
26956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
9808 KB |
Output is correct |
2 |
Correct |
2 ms |
9808 KB |
Output is correct |
3 |
Correct |
3 ms |
9976 KB |
Output is correct |
4 |
Correct |
5 ms |
8016 KB |
Output is correct |
5 |
Correct |
5 ms |
8032 KB |
Output is correct |
6 |
Correct |
5 ms |
8024 KB |
Output is correct |
7 |
Correct |
6 ms |
8024 KB |
Output is correct |
8 |
Correct |
6 ms |
8244 KB |
Output is correct |
9 |
Correct |
5 ms |
8028 KB |
Output is correct |
10 |
Correct |
68 ms |
19272 KB |
Output is correct |
11 |
Correct |
70 ms |
17864 KB |
Output is correct |
12 |
Correct |
43 ms |
19016 KB |
Output is correct |
13 |
Correct |
36 ms |
16080 KB |
Output is correct |
14 |
Correct |
53 ms |
26952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
9040 KB |
Output is correct |
2 |
Correct |
27 ms |
11856 KB |
Output is correct |
3 |
Correct |
31 ms |
11612 KB |
Output is correct |
4 |
Correct |
22 ms |
11864 KB |
Output is correct |
5 |
Correct |
11 ms |
10584 KB |
Output is correct |
6 |
Correct |
25 ms |
14428 KB |
Output is correct |
7 |
Correct |
25 ms |
18008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
9808 KB |
Output is correct |
2 |
Correct |
2 ms |
9808 KB |
Output is correct |
3 |
Correct |
3 ms |
9976 KB |
Output is correct |
4 |
Correct |
5 ms |
8016 KB |
Output is correct |
5 |
Correct |
5 ms |
8032 KB |
Output is correct |
6 |
Correct |
5 ms |
8024 KB |
Output is correct |
7 |
Correct |
6 ms |
8024 KB |
Output is correct |
8 |
Correct |
6 ms |
8244 KB |
Output is correct |
9 |
Correct |
5 ms |
8028 KB |
Output is correct |
10 |
Correct |
5 ms |
8284 KB |
Output is correct |
11 |
Correct |
6 ms |
8288 KB |
Output is correct |
12 |
Correct |
5 ms |
8284 KB |
Output is correct |
13 |
Correct |
47 ms |
26972 KB |
Output is correct |
14 |
Correct |
81 ms |
30808 KB |
Output is correct |
15 |
Correct |
71 ms |
26964 KB |
Output is correct |
16 |
Correct |
68 ms |
19272 KB |
Output is correct |
17 |
Correct |
70 ms |
17864 KB |
Output is correct |
18 |
Correct |
43 ms |
19016 KB |
Output is correct |
19 |
Correct |
36 ms |
16080 KB |
Output is correct |
20 |
Correct |
53 ms |
26952 KB |
Output is correct |
21 |
Correct |
21 ms |
12112 KB |
Output is correct |
22 |
Correct |
65 ms |
20808 KB |
Output is correct |
23 |
Correct |
73 ms |
25160 KB |
Output is correct |
24 |
Correct |
117 ms |
35912 KB |
Output is correct |
25 |
Correct |
52 ms |
19732 KB |
Output is correct |
26 |
Correct |
102 ms |
22436 KB |
Output is correct |
27 |
Correct |
75 ms |
21364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
9808 KB |
Output is correct |
2 |
Correct |
2 ms |
9808 KB |
Output is correct |
3 |
Correct |
3 ms |
9976 KB |
Output is correct |
4 |
Correct |
5 ms |
8016 KB |
Output is correct |
5 |
Correct |
5 ms |
8032 KB |
Output is correct |
6 |
Correct |
5 ms |
8024 KB |
Output is correct |
7 |
Correct |
6 ms |
8024 KB |
Output is correct |
8 |
Correct |
6 ms |
8244 KB |
Output is correct |
9 |
Correct |
5 ms |
8028 KB |
Output is correct |
10 |
Correct |
68 ms |
19024 KB |
Output is correct |
11 |
Correct |
51 ms |
19832 KB |
Output is correct |
12 |
Correct |
129 ms |
35460 KB |
Output is correct |
13 |
Correct |
73 ms |
19920 KB |
Output is correct |
14 |
Correct |
95 ms |
21532 KB |
Output is correct |
15 |
Correct |
5 ms |
8284 KB |
Output is correct |
16 |
Correct |
6 ms |
8288 KB |
Output is correct |
17 |
Correct |
5 ms |
8284 KB |
Output is correct |
18 |
Correct |
47 ms |
26972 KB |
Output is correct |
19 |
Correct |
81 ms |
30808 KB |
Output is correct |
20 |
Correct |
71 ms |
26964 KB |
Output is correct |
21 |
Correct |
51 ms |
15188 KB |
Output is correct |
22 |
Correct |
51 ms |
14420 KB |
Output is correct |
23 |
Correct |
45 ms |
19028 KB |
Output is correct |
24 |
Correct |
39 ms |
16076 KB |
Output is correct |
25 |
Correct |
45 ms |
26956 KB |
Output is correct |
26 |
Correct |
68 ms |
19272 KB |
Output is correct |
27 |
Correct |
70 ms |
17864 KB |
Output is correct |
28 |
Correct |
43 ms |
19016 KB |
Output is correct |
29 |
Correct |
36 ms |
16080 KB |
Output is correct |
30 |
Correct |
53 ms |
26952 KB |
Output is correct |
31 |
Correct |
9 ms |
9040 KB |
Output is correct |
32 |
Correct |
27 ms |
11856 KB |
Output is correct |
33 |
Correct |
31 ms |
11612 KB |
Output is correct |
34 |
Correct |
22 ms |
11864 KB |
Output is correct |
35 |
Correct |
11 ms |
10584 KB |
Output is correct |
36 |
Correct |
25 ms |
14428 KB |
Output is correct |
37 |
Correct |
25 ms |
18008 KB |
Output is correct |
38 |
Correct |
21 ms |
12112 KB |
Output is correct |
39 |
Correct |
65 ms |
20808 KB |
Output is correct |
40 |
Correct |
73 ms |
25160 KB |
Output is correct |
41 |
Correct |
117 ms |
35912 KB |
Output is correct |
42 |
Correct |
52 ms |
19732 KB |
Output is correct |
43 |
Correct |
102 ms |
22436 KB |
Output is correct |
44 |
Correct |
75 ms |
21364 KB |
Output is correct |
45 |
Correct |
21 ms |
11600 KB |
Output is correct |
46 |
Correct |
65 ms |
20004 KB |
Output is correct |
47 |
Correct |
77 ms |
23296 KB |
Output is correct |
48 |
Correct |
108 ms |
32340 KB |
Output is correct |
49 |
Correct |
68 ms |
19980 KB |
Output is correct |
50 |
Correct |
88 ms |
21844 KB |
Output is correct |
51 |
Correct |
79 ms |
20820 KB |
Output is correct |