#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=100007;
vector<int>G[N];
ll w[N];
int a[N];
int s[N];
int nx[N];
set<pair<int,ll>>S[N];
void dfs(int v)
{
s[v]=1;
nx[v]=v;
int mx=0;
for(auto u:G[v])
{
dfs(u);
s[v]+=s[u];
if(s[u]>mx)
{
mx=s[u];
nx[v]=nx[u];
}
}
S[nx[v]].insert({inf,infl});
for(auto u:G[v])
{
if(nx[u]==nx[v]) continue;
for(auto x:S[nx[u]])
{
pair<int,ll>p=*S[nx[v]].lower_bound({x.st,0});
if(p.st==x.st)
{
S[nx[v]].erase(p);
S[nx[v]].insert({p.st,p.nd+x.nd});
}
else S[nx[v]].insert(x);
}
}
if(a[v]>0)
{
ll r=w[v];
while(r>0)
{
pair<int,ll>p=*S[nx[v]].lower_bound({a[v],infl});
if(p.st==inf) break;
S[nx[v]].erase(p);
if(r>=p.nd) r-=p.nd;
else
{
S[nx[v]].insert({p.st,p.nd-r});
break;
}
}
pair<int,ll>p=*S[nx[v]].lower_bound({a[v],0});
if(p.st==a[v])
{
S[nx[v]].erase(p);
S[nx[v]].insert({p.st,p.nd+w[v]});
}
else S[nx[v]].insert({a[v],w[v]});
}
S[nx[v]].erase({inf,infl});
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m,k,p;
cin>>n>>m>>k;
for(int i=2;i<=n;i++)
{
cin>>p;
G[p].pb(i);
}
while(m--)
{
int i;
cin>>i>>a[i]>>w[i];
}
dfs(1);
ll ans=0;
for(auto x:S[nx[1]]) ans+=x.nd;
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
6 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7272 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
19804 KB |
Output is correct |
2 |
Correct |
57 ms |
22372 KB |
Output is correct |
3 |
Correct |
142 ms |
36820 KB |
Output is correct |
4 |
Correct |
85 ms |
21076 KB |
Output is correct |
5 |
Correct |
125 ms |
23500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7508 KB |
Output is correct |
2 |
Correct |
4 ms |
7588 KB |
Output is correct |
3 |
Correct |
4 ms |
7636 KB |
Output is correct |
4 |
Correct |
64 ms |
33140 KB |
Output is correct |
5 |
Correct |
153 ms |
37076 KB |
Output is correct |
6 |
Correct |
80 ms |
33132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
16732 KB |
Output is correct |
2 |
Correct |
68 ms |
15892 KB |
Output is correct |
3 |
Correct |
75 ms |
22492 KB |
Output is correct |
4 |
Correct |
50 ms |
17000 KB |
Output is correct |
5 |
Correct |
57 ms |
33312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
6 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7272 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7376 KB |
Output is correct |
10 |
Correct |
89 ms |
18908 KB |
Output is correct |
11 |
Correct |
87 ms |
17948 KB |
Output is correct |
12 |
Correct |
76 ms |
21832 KB |
Output is correct |
13 |
Correct |
59 ms |
16356 KB |
Output is correct |
14 |
Correct |
69 ms |
32656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8404 KB |
Output is correct |
2 |
Correct |
42 ms |
11608 KB |
Output is correct |
3 |
Correct |
39 ms |
11604 KB |
Output is correct |
4 |
Correct |
35 ms |
11732 KB |
Output is correct |
5 |
Correct |
15 ms |
10068 KB |
Output is correct |
6 |
Correct |
36 ms |
15072 KB |
Output is correct |
7 |
Correct |
42 ms |
19884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
6 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7272 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7376 KB |
Output is correct |
10 |
Correct |
4 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7588 KB |
Output is correct |
12 |
Correct |
4 ms |
7636 KB |
Output is correct |
13 |
Correct |
64 ms |
33140 KB |
Output is correct |
14 |
Correct |
153 ms |
37076 KB |
Output is correct |
15 |
Correct |
80 ms |
33132 KB |
Output is correct |
16 |
Correct |
89 ms |
18908 KB |
Output is correct |
17 |
Correct |
87 ms |
17948 KB |
Output is correct |
18 |
Correct |
76 ms |
21832 KB |
Output is correct |
19 |
Correct |
59 ms |
16356 KB |
Output is correct |
20 |
Correct |
69 ms |
32656 KB |
Output is correct |
21 |
Correct |
26 ms |
10964 KB |
Output is correct |
22 |
Correct |
91 ms |
19956 KB |
Output is correct |
23 |
Correct |
100 ms |
23268 KB |
Output is correct |
24 |
Correct |
145 ms |
31200 KB |
Output is correct |
25 |
Correct |
113 ms |
20456 KB |
Output is correct |
26 |
Correct |
136 ms |
23516 KB |
Output is correct |
27 |
Correct |
98 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
6 ms |
7376 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
5 ms |
7272 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7376 KB |
Output is correct |
10 |
Correct |
73 ms |
19804 KB |
Output is correct |
11 |
Correct |
57 ms |
22372 KB |
Output is correct |
12 |
Correct |
142 ms |
36820 KB |
Output is correct |
13 |
Correct |
85 ms |
21076 KB |
Output is correct |
14 |
Correct |
125 ms |
23500 KB |
Output is correct |
15 |
Correct |
4 ms |
7508 KB |
Output is correct |
16 |
Correct |
4 ms |
7588 KB |
Output is correct |
17 |
Correct |
4 ms |
7636 KB |
Output is correct |
18 |
Correct |
64 ms |
33140 KB |
Output is correct |
19 |
Correct |
153 ms |
37076 KB |
Output is correct |
20 |
Correct |
80 ms |
33132 KB |
Output is correct |
21 |
Correct |
70 ms |
16732 KB |
Output is correct |
22 |
Correct |
68 ms |
15892 KB |
Output is correct |
23 |
Correct |
75 ms |
22492 KB |
Output is correct |
24 |
Correct |
50 ms |
17000 KB |
Output is correct |
25 |
Correct |
57 ms |
33312 KB |
Output is correct |
26 |
Correct |
89 ms |
18908 KB |
Output is correct |
27 |
Correct |
87 ms |
17948 KB |
Output is correct |
28 |
Correct |
76 ms |
21832 KB |
Output is correct |
29 |
Correct |
59 ms |
16356 KB |
Output is correct |
30 |
Correct |
69 ms |
32656 KB |
Output is correct |
31 |
Correct |
10 ms |
8404 KB |
Output is correct |
32 |
Correct |
42 ms |
11608 KB |
Output is correct |
33 |
Correct |
39 ms |
11604 KB |
Output is correct |
34 |
Correct |
35 ms |
11732 KB |
Output is correct |
35 |
Correct |
15 ms |
10068 KB |
Output is correct |
36 |
Correct |
36 ms |
15072 KB |
Output is correct |
37 |
Correct |
42 ms |
19884 KB |
Output is correct |
38 |
Correct |
26 ms |
10964 KB |
Output is correct |
39 |
Correct |
91 ms |
19956 KB |
Output is correct |
40 |
Correct |
100 ms |
23268 KB |
Output is correct |
41 |
Correct |
145 ms |
31200 KB |
Output is correct |
42 |
Correct |
113 ms |
20456 KB |
Output is correct |
43 |
Correct |
136 ms |
23516 KB |
Output is correct |
44 |
Correct |
98 ms |
23764 KB |
Output is correct |
45 |
Correct |
26 ms |
11328 KB |
Output is correct |
46 |
Correct |
94 ms |
21092 KB |
Output is correct |
47 |
Correct |
102 ms |
24468 KB |
Output is correct |
48 |
Correct |
150 ms |
34200 KB |
Output is correct |
49 |
Correct |
109 ms |
21096 KB |
Output is correct |
50 |
Correct |
147 ms |
24452 KB |
Output is correct |
51 |
Correct |
127 ms |
24596 KB |
Output is correct |