vijos:p1706 (Prom)

Source: Internet
Author: User

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)

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.