Submission #236928

#TimeUsernameProblemLanguageResultExecution timeMemory
236928mohamedsobhi777Biochips (IZhO12_biochips)C++14
0 / 100
2058 ms403448 KiB
#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);
                for (int j = k; j >= 0 ; j--)
                {
                        for (int k1 = 0; k1 + j <=k ; k1++)
                        {
                                dp[x][j+k1] = max(dp[x][j+k1], dp[u][k1] + dp[x][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)

biochips.cpp: In function 'void dfs(int)':
biochips.cpp:15:13: warning: unused variable 'ret' [-Wunused-variable]
         int ret = 0;    
             ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...