#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n,i,j,k,l,r,x,y,z,w,s,t,b[1100000],dp[110000][1100],h[1100000],m;
vector<ll> v[1100000],u;
pair<ll,ll> a[1100000];
void f(ll x,ll y)
{
ll i,j;
for(i=0;i<h[x];i++)
{
if(v[x][i]==y)
continue;
f(v[x][i],x);
}
for(i=1;i<=k;i++)
{
s=0;
for(j=0;j<h[x];j++)
{
if(v[x][j]==y)
continue;
if(i==1)
b[j]=0;
b[j]=max(b[j],dp[v[x][j]][i]);
s+=b[j];
}
if(i==a[x].first)
s+=a[x].second;
dp[x][i]=s;
}
}
int main()
{
scanf("%lld %lld %lld",&n,&m,&k);
for(i=2;i<=n;i++)
{
scanf("%lld",&x);
y=i;
v[x].push_back(y);
v[y].push_back(x);
h[x]++;
h[y]++;
}
for(i=1;i<=m;i++)
{
scanf("%lld %lld %lld",&x,&y,&z);
u.push_back(y);
a[x]={y,z};
}
sort(u.begin(),u.end());
u.erase(unique(u.begin(),u.end()),u.end());
for(i=1;i<=m;i++)
{
x=i;
a[x].first=lower_bound(u.begin(),u.end(),a[x].first)-u.begin()+1;
}
k=min(k,m);
f(1,0);
for(i=1;i<=k;i++)
{
s=max(s,dp[1][i]);
}
printf("%lld",s);
}
Compilation message
magictree.cpp: In function 'int main()':
magictree.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%lld %lld %lld",&n,&m,&k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:38:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%lld",&x);
| ~~~~~^~~~~~~~~~~
magictree.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%lld %lld %lld",&x,&y,&z);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
31064 KB |
Output is correct |
2 |
Correct |
8 ms |
31068 KB |
Output is correct |
3 |
Incorrect |
8 ms |
31064 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2047 ms |
899368 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
39516 KB |
Output is correct |
2 |
Incorrect |
12 ms |
39560 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
899784 KB |
Output is correct |
2 |
Correct |
202 ms |
899632 KB |
Output is correct |
3 |
Correct |
186 ms |
902596 KB |
Output is correct |
4 |
Correct |
170 ms |
901996 KB |
Output is correct |
5 |
Correct |
167 ms |
906944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
31064 KB |
Output is correct |
2 |
Correct |
8 ms |
31068 KB |
Output is correct |
3 |
Incorrect |
8 ms |
31064 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
103 ms |
206556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
31064 KB |
Output is correct |
2 |
Correct |
8 ms |
31068 KB |
Output is correct |
3 |
Incorrect |
8 ms |
31064 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
31064 KB |
Output is correct |
2 |
Correct |
8 ms |
31068 KB |
Output is correct |
3 |
Incorrect |
8 ms |
31064 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |