# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
89376 | Vardanyan | Biochips (IZhO12_biochips) | C++14 | 889 ms | 400376 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 200*1000+5;
vector<int> g[N];
int X[N];
int n,m;
int dp[N][505];
int knap[N][505];
int ans = 0;
void dfs(int v,int p = -1){
dp[v][1] = X[v];
for(int i = 0;i<g[v].size();i++){
int to = g[v][i];
if(to == p) continue;
dfs(to,v);
}
int cnt = 0;
memset(knap,0,sizeof(knap));
for(int i = 0;i<g[v].size();i++){
if(g[v][i] == p) continue;
cnt++;
for(int x = m;x>=1;x--){
for(int y = x;y>=1;y--){
for(int pos = 0;pos<cnt;pos++){
knap[cnt][x] = max(knap[cnt][x],knap[pos][x-y]+dp[g[v][i]][y]);
dp[g[v][i]][x] = max(dp[g[v][i]][x],knap[cnt][x]);
if(x == m) ans = max(ans,dp[g[v][i]][x]);
}
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
int root;
for(int i = 1;i<=n;i++){
int v,x;
scanf("%d%d",&v,&x);
X[i] = x;
if(v == 0) {
root = i;
continue;
}
g[v].push_back(i);
g[i].push_back(v);
}
dfs(root);
printf("%d\n",ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |