#include<bits/stdc++.h>
using namespace std;
const int N=200000;
int n,k;
vector<int> v(N);
vector<vector<int>> adj(N);
vector<vector<int>> dp(N,vector<int>(501));
vector<int> leaf(N);
void dfs(int k){
if(adj[k].size()==0){
leaf[k]=1;
}
for(int u:adj[k]){
dfs(u);
leaf[k]+=leaf[u];
for(int i=min(leaf[k],500);i>0;i--){
for(int j=0;j<=min(leaf[j],i);j++){
dp[k][i]=max(dp[k][i],dp[u][j]+dp[k][i-j]);
}
}
}
dp[k][1]=max(dp[k][1],v[k]);
return;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int root=0;
cin>>n>>k;
for(int i=0;i<n;i++){
int p;cin>>p;p--;
if(p!=-1){
adj[p].push_back(i);
}
else{
root=i;
}
cin>>v[i];
}
dfs(root);
cout<<dp[root][k]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
210 ms |
405772 KB |
Output isn't correct |
2 |
Correct |
219 ms |
405800 KB |
Output is correct |
3 |
Incorrect |
218 ms |
405768 KB |
Output isn't correct |
4 |
Incorrect |
209 ms |
405960 KB |
Output isn't correct |
5 |
Incorrect |
239 ms |
405936 KB |
Output isn't correct |
6 |
Incorrect |
211 ms |
405920 KB |
Output isn't correct |
7 |
Incorrect |
386 ms |
408392 KB |
Output isn't correct |
8 |
Incorrect |
380 ms |
408384 KB |
Output isn't correct |
9 |
Incorrect |
692 ms |
408252 KB |
Output isn't correct |
10 |
Incorrect |
747 ms |
408412 KB |
Output isn't correct |