| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1291208 | gramathegod | Biochips (IZhO12_biochips) | C++20 | 2 ms | 576 KiB |
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+5, M=505;
vector<int>adj[N];
int dp[N][M], sz[N], val[N], n,m,root;
void getsz(int i){
for (auto x:adj[i]){
getsz(x);
sz[i]+=sz[x];
}
if (adj[i].size()==0){
sz[i]=1;
}
}
void dfs(int i){
int maxj=min(m,sz[i]);
for (auto x:adj[i]){
dfs(x);
int maxk=min(m,sz[x]);
for (int j=maxj;j>=0;j--){
for (int k=min(maxk,j);k>=0;k--){
dp[i][j]=max(dp[i][j], dp[i][j-k]+dp[x][k]);
}
}
}
dp[i][1]=max(dp[i][1], val[i]);
}
int main()
{
freopen("d.in", "r", stdin);
freopen("d.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m;
for (int i=1;i<=n;i++){
int p;cin>>p>>val[i];
if (p==0){
root=i;
}
adj[p].push_back(i);
}
getsz(root);
dfs(root);
cout<<dp[root][m];
return 0;
}
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
