Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstring>
- using namespace std;
- const int maxn=5005;
- vector<int>graph[maxn];
- int arr[maxn];
- int parent[maxn];
- int dp[maxn];
- int dfs(int pos){
- if(dp[pos]!=-1){
- return dp[pos];
- }
- int r1=arr[pos];
- for(int i=0; i<graph[pos].size(); i++){
- int sosed=graph[pos][i];
- for(int j=0; j<graph[sosed].size(); j++){
- int sosed_2=graph[sosed][j];
- r1+=dfs(sosed_2);
- }
- }
- int r2=0;
- for(int i=0; i<graph[pos].size(); i++){
- r2+=dfs(graph[pos][i]);
- }
- return dp[pos]=max(r1, r2);
- }
- int main()
- {
- int n;
- cin>>n>>arr[0];
- int a, b;
- for(int i=1; i<n; i++){
- cin>>arr[i]>>b;
- a=i;
- parent[i]=b;
- graph[b].push_back(i);
- }
- memset(dp, -1, sizeof dp);
- cout<<dfs(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement