# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
673294 | Baytoro | Biochips (IZhO12_biochips) | C++17 | 550 ms | 524288 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;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define int long long
#define endl '\n'
#define cnt(x) __builtin_popcount(x)
void fopn(string name){
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
const int INF=1e18,mod=998244353;
int n,m;
const int N=2e5+5;
int a[N],sz[N];
int dp[N][505];
vector<int> tmp;
vector<int> g[N];
void dfs(int x, int p){
sz[x]=1;
dp[x][0]=0;
for(auto it: g[x]){
if(it==p) continue;
dfs(it,x);
tmp.assign(m+1,0);
for(int i=0;i<=min(sz[x],m);i++)
for(int j=0;j<=min(m-i,sz[it]);j++)
tmp[i+j]=max(tmp[i+j],dp[x][i]+dp[it][j]);
sz[x]+=sz[it];
for(int i=1;i<=min(m,sz[x]);i++)
dp[x][i]=tmp[i];
}
dp[x][1]=max(dp[x][1],a[x]);
}
void solve(){
cin>>n>>m;
int r=0;
for(int i=1;i<=n;i++){
int p;cin>>p>>a[i];
if(!p)r=i;
else{
g[p].pb(i);
g[i].pb(p);
}
}
dfs(r,r);
cout<<dp[r][m]<<endl;
}
main(){
//fopn("newbarn");
ios;
int T=1;
//cin>>T;
while(T--){
solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |