#include "bits/stdc++.h"
#define MAXN 200009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int arr[MAXN],go[MAXN],cost[MAXN],dp[2][MAXN],TIM,id[MAXN];
vector<int>adj[MAXN];
void dfs(int nd){
int tm=++TIM;
id[tm]=nd;
tr(it,adj[nd])
dfs(*it);
go[tm]=TIM;
}
int main(){
//~ #ifndef ONLINE_JUDGE
//~ freopen("file.in", "r", stdin);
//~ #endif
int n,m,root;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int p;
scanf("%d%d",&p,&arr[i]);
if(p)
adj[p].pb(i);
else
root=i;
}dfs(root);
for(int i=1;i<=n;i++)
dp[1][i]=arr[id[i]];
for(int i=2;i<=m;i++){cost[n+1]=-INF;
for(int j=n;j>=1;j--)
cost[j]=max(cost[j+1],dp[(i-1)&1][j]);
for(int j=n;j>=1;j--)
dp[i&1][j]=cost[go[j]+1]+arr[id[j]];
}
int ans=0;
for(int i=1;i<=n;i++)
umax(ans,dp[m&1][i]);
printf("%d\n",ans) ;
return 0;
}
Compilation message
biochips.cpp: In function 'int main()':
biochips.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
biochips.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d%d",&p,&arr[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~
biochips.cpp:42:6: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
42 | }dfs(root);
| ~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
6 ms |
9012 KB |
Output is correct |
5 |
Correct |
7 ms |
8796 KB |
Output is correct |
6 |
Correct |
9 ms |
8796 KB |
Output is correct |
7 |
Correct |
230 ms |
10544 KB |
Output is correct |
8 |
Correct |
240 ms |
10544 KB |
Output is correct |
9 |
Correct |
322 ms |
10716 KB |
Output is correct |
10 |
Correct |
449 ms |
10868 KB |
Output is correct |