[Codevs 1380] Prom without a boss

Source: Internet
Author: User

"Problem description"

Ural University has a staff of N and is numbered 1~n. They have affiliation, which means that their relationship is like a tree rooted in the headmaster, and the parent node is the direct boss of the child node. Each employee has a happiness index. There is now an anniversary party, which asks the staff to have the most happiness index. However, no staff is willing to attend with the direct boss.

Input

The first line is an integer n. (1<=n<=6000)
Next n lines, line i+1 represents the happiness index RI for staff i. ( -128<=ri<=127)
Next N-1 line, enter a pair of integer l, K for each line. Indicates that K is the direct boss of L.
Last line input 0, 0.

Output

Output the maximum happiness index.

"Algorithmic Analysis"

  Similar to the 0-1 Backpack tree DP Basic problem, state transfer is described in the code.

"Program Code"

1 varA,FA:Array[1..6000] ofLongint;2F:Array[1..6000,0..1] ofLongint;3SArray[1..6000] ofLongint;4 I,j,k,n,m,ans,u,v:longint;5 functionMax (x,y:longint): Longint;6 begin7     ifX>y ThenExit (x)Elseexit (y);8 End;9 procedureDP (x:longint);Ten varI:longint; One begin A     ifs[x]=0  Then beginF[x,0]:=0; F[x,1]:=A[X]; ExitEnd; -      fori:=1  toN Do ifFa[i]=x Then -     begin the DP (i); -Inc (F[X,0],max (F[i,1],f[i,0])); -Inc (F[X,1],f[i,0]); -     End; + End; - begin + READLN (n); A      fori:=1  toN Doreadln (A[i]); at      fori:=1  toN-1  Do -     begin - readln (u,v); -fa[u]:=v; - Inc (S[v]); -     End; in readln (u,v); -      fori:=1  toN Do iffa[i]=0  Thenk:=i; to      fori:=1  toN DoF[i,1]:=A[i]; + DP (k); -Writeln (Max (F[k,1],f[k,0])); the End.

Disclaimer: This blog post for Blogger original blog, do not reprint without permission.

[Codevs 1380] No boss's prom

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.