# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
483911 | FatihSolak | Biochips (IZhO12_biochips) | C++17 | 169 ms | 11616 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>
#define N 200005
using namespace std;
int n,m;
vector<int> adj[N];
int par[N];
int arr[N];
int ans = 0;
vector<int> dfs(int v,int c,vector<int> dp){
dp[0] = 0;
vector<int> tmp = dp;
for(auto u:adj[v]){
if(u == c)continue;
dp = dfs(u,-1,dp);
}
if(c == -1){
for(int i=1;i<=m;i++){
dp[i] = max(dp[i],tmp[i-1]+arr[v]);
}
}
else{
dp[1] = max(dp[1],arr[v]);
}
ans = max(ans,dp[m]);
if(c != -1 && par[v]){
dfs(par[v],v,dp);
}
return dp;
}
void solve(){
cin >> n >> m;
int root = 0;
for(int i=1;i<=n;i++){
cin >> par[i] >> arr[i];
if(par[i] == 0)root = i;
else adj[par[i]].push_back(i);
}
for(int i=1;i<=n;i++){
if(adj[i].empty()){
dfs(i,0,vector<int>(m+1,-1e9));
break;
}
}
cout << ans << endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |