Submission #827437

#TimeUsernameProblemLanguageResultExecution timeMemory
827437MasterTasterBiochips (IZhO12_biochips)C++14
50 / 100
2110 ms312080 KiB
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> #define xx first #define yy second #define pb push_back #define MAXN 200010 #define MAXM 510 #define MAXX 100000010 using namespace std; ll n, m, p[MAXN], w[MAXN], dp[MAXN][MAXM]; bool bio[MAXN]; vector<int> g[MAXN]; void merge(int u, int v) { ll pomoc[MAXM]; for (int i=0; i<=m; i++) pomoc[i]=dp[u][i]; for (int i=0; i<=m; i++) for (int j=0; i+j<=m; j++) if (i+j<=m) pomoc[i+j]=max(pomoc[i+j], dp[u][i]+dp[v][j]); for (int i=0; i<=m; i++) dp[u][i]=pomoc[i]; } void dfs(int u) { bio[u]=true; for (int i=0; i<=m; i++) dp[u][i]=-MAXX; dp[u][0]=0; for (auto v:g[u]) { if (!bio[v]) { dfs(v); } } for (auto v:g[u]) { if (v==p[u]) continue; merge(u, v); } dp[u][1]=max(dp[u][1], w[u]); ///cout<<u<<":"<<endl; ///for (int i=0; i<=m; i++) cout<<dp[u][i]<<" "; ///cout<<endl; } int main(){ cin>>n>>m; int koren=1; for (int i=1; i<=n; i++) { cin>>p[i]>>w[i]; if (p[i]==0) { koren=i; continue; } g[p[i]].pb(i); g[i].pb(p[i]); } dfs(koren); cout<<dp[koren][m]; }
#Verdict Execution timeMemoryGrader output
Fetching results...