// el psy congroo
#include <bits/stdc++.h>
using namespace std;
#define int int
#define ins insert
#define pii pair<int,int>
#define pb push_back
#define endl '\n'
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
const int mod = 1e9 +7, sze = 2e5 +1, inf = 0, LG = 20;
vector<int> graph[sze];
int val[sze];
int k;
int dp[sze][501];
int mx[sze];
void dfs(int node,int par=-1){
int a= 0,b;
for(auto v:graph[node]){
if(v!=par){
dfs(v,node);
b = mx[v];
a=min(a,k);
b=min(b,k);
for(int i = a;i>=0;i--){
if( (!i) || dp[node][i]){
for(int j= 0;j<=b && ( (i+j)<=k);j++){
if(!(j) || dp[v][j]){
dp[node][i+j]=max(dp[node][i+j],dp[node][i] + dp[v][j]);
}
}
}
}
a+=b;
mx[node]=max(mx[node],a);
}
}
dp[node][1]=max(dp[node][1],val[node]);
mx[node]=max(mx[node],(int)1);
}
void fast(){
int n,p,w;
cin>>n>>k;
int root =0;
for(int i=1;i<=n;i++){
p,w;
cin>>p>>w;
root|=(!p) * i;
if(p!=0){
graph[p].pb(i);
graph[i].pb(p);
}
val[i]=w;
}
dfs(root);
cout<<dp[root][k]<<endl;
}
signed main(){
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
int tt = 1;
// cin>>tt;
while(tt--){
fast();
}
return 0;
}
Compilation message
biochips.cpp: In function 'void fast()':
biochips.cpp:48:9: warning: left operand of comma operator has no effect [-Wunused-value]
48 | p,w;
| ^
biochips.cpp:48:12: warning: right operand of comma operator has no effect [-Wunused-value]
48 | p,w;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6736 KB |
Output is correct |
4 |
Correct |
18 ms |
24572 KB |
Output is correct |
5 |
Correct |
19 ms |
26872 KB |
Output is correct |
6 |
Correct |
22 ms |
26704 KB |
Output is correct |
7 |
Correct |
378 ms |
302160 KB |
Output is correct |
8 |
Correct |
382 ms |
302244 KB |
Output is correct |
9 |
Correct |
480 ms |
367176 KB |
Output is correct |
10 |
Correct |
606 ms |
405064 KB |
Output is correct |