# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
236929 | mohamedsobhi777 | Biochips (IZhO12_biochips) | C++14 | 887 ms | 403704 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 = 2e5 + 7;
int n, k;
int root;
int dp[N][505];
int a[N];
vector<int> adj[N];
void dfs(int x)
{
int ret = 0;
dp[x][0] = 0;
for (auto u : adj[x])
{
dfs(u);
int mxx = 0 ;
vector<int> auxdp (k+1 , 0) ;
for (int j = 0; j <= k ; j++)
{
if(dp[j] < 0) break;
for (int k1 = 0; k1 + j <=k ; k1++)
{
if(dp[u][k1] < 0) break ;
mxx = max(mxx , k1 + j) ;
auxdp[j+k1] = max(auxdp[j+k1] , dp[x][j] + dp[u][k1]) ;
}
}
for(int j = 0 ;j <= k; j++){
dp[x][j] = max(dp[x][j] , auxdp[j]) ;
}
}
dp[x][1] = max(dp[x][1] , a[x]) ;
return ;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("in.in", "r", stdin);
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
int u ;
cin >> u >> a[i];
if (u)
adj[u].push_back(i);
else
root = i;
}
memset(dp, -1, sizeof dp);
dfs(root) ;
cout<< dp[root][k] ;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |