#pragma GCC optimize("Ofast","O3","unroll-loops")
#pragma GCC target("avx2")
#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(501,vector<int>(N));
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=1;j<=min(leaf[u],i);j++){
dp[i][k]=max(dp[i][k],dp[j][u]+dp[i-j][k]);
}
}
}
dp[1][k]=max(dp[1][k],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[k][root]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
400076 KB |
Output is correct |
2 |
Correct |
150 ms |
400044 KB |
Output is correct |
3 |
Correct |
159 ms |
400044 KB |
Output is correct |
4 |
Correct |
177 ms |
400188 KB |
Output is correct |
5 |
Correct |
187 ms |
400188 KB |
Output is correct |
6 |
Correct |
180 ms |
400264 KB |
Output is correct |
7 |
Correct |
702 ms |
401284 KB |
Output is correct |
8 |
Correct |
745 ms |
401284 KB |
Output is correct |
9 |
Execution timed out |
2084 ms |
401244 KB |
Time limit exceeded |
10 |
Execution timed out |
2078 ms |
401168 KB |
Time limit exceeded |