#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[505][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][j]);
for(int j=n;j>=1;j--)
umax(dp[i][j],cost[go[j]+1]+arr[id[j]]);
}
int ans=0;
for(int i=1;i<=n;i++)
umax(ans,dp[m][i]);
printf("%d\n",ans) ;
return 0;
}
Compilation message
biochips.cpp: In function 'int main()':
biochips.cpp:34:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
^
biochips.cpp:37:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&p,&arr[i]);
^
biochips.cpp:42:12: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
}dfs(root);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
404380 KB |
Output is correct |
2 |
Correct |
0 ms |
404380 KB |
Output is correct |
3 |
Correct |
3 ms |
404380 KB |
Output is correct |
4 |
Correct |
9 ms |
404512 KB |
Output is correct |
5 |
Correct |
3 ms |
404512 KB |
Output is correct |
6 |
Correct |
9 ms |
404512 KB |
Output is correct |
7 |
Correct |
349 ms |
405436 KB |
Output is correct |
8 |
Correct |
383 ms |
405436 KB |
Output is correct |
9 |
Correct |
526 ms |
405436 KB |
Output is correct |
10 |
Correct |
853 ms |
405436 KB |
Output is correct |