Describe
Arthur Company is a hierarchical company, they have a strict relationship between the boss and subordinates, the company to the President for the highest position, he has a number of subordinates, his subordinates have a number of subordinates, his subordinates have a number of subordinates ... is close to the end, the company organized gathering activities, there is a part of the event is a free dance, the company's staff have a funny value, now you have to develop a set of who the program, so that all actors on the stage the funniest value of the biggest. Of course, the staff are not going onstage with their immediate superiors.
Format input Format
The first line, an integer n, represents the total number of employees in the company.
The next line has n integers, separated by spaces, and the first integer represents the funny value Ai ( -1327670≤ai≤1327670) of staff I.
Next N-1 line, each line a 1 to n integer, the first integer indicates who the staff i+1 's immediate supervisor, of course, the president is staff 1.
Output format
An integer that represents the maximum value of the sum of the funny values of all staff on the platform.
Input:
7
1 1 1 1 1 1 1
1
1
5
1
4
4
Output:
5
Without the boss's prom question.
#include <iostream>#include<algorithm>#include<vector>using namespacestd;Const intmaxn=5005; typedefLong Longll;intN;intV[maxn];vector<int>tree[maxn];ll dp[maxn][2];voidDfsintu) {dp[u][1]+=V[u]; for(intI=0; I<tree[u].size (); i++) { intv=Tree[u][i]; DFS (v); dp[u][0]+=max (dp[v][1],dp[v][0]); dp[u][1]+=dp[v][0]; }}intMain () {CIN>>N; for(intI=1; i<=n;i++) {cin>>V[i]; } for(intI=2; i<=n;i++) { intFA; CIN>>FA; Tree[fa].push_back (i); } DFS (1); cout<<max (dp[1][0],dp[1][1]) <<Endl; return 0;}
vijos:p1706 (Prom)