Pruning flowers
time limit: 1 sspace limit: 256000 KBTitle Description
Description
ZZ is interested in mathematics, and is a studious student, always stay in the classroom after class to ask the teacher some questions.
One day he rode to class in the morning, on the way to see an old man is pruning flowers and grass, suddenly think of a question about pruning flowers.
Then the day after the lesson, ZZ asked the teacher the question:
A strange flower, with a total of n flowers on it, and a total of N-1 branches to connect the flowers together, and without trimming every
Flowers are not isolated.
Each flower has a "beautiful index", the larger the number indicates that the more beautiful flower, there are "beautiful index" is negative,
This flower is disgusting to look at.
The so-called "pruning", meaning: remove one of the branches, so that a flower has become two strains, throw away one of the strains.
After a series of "pruning", the last flower (or perhaps one) remains.
The teacher's task is to make the remaining strain (which is not done by a series of "trims")
Flower) flowers on the "Beauty Index" and the largest.
The teacher thought for a while, gave the positive solution (the teacher of the big school is very cow ~). ZZ See the problem is easily broken, quite uncomfortable,
So I asked you again.
Enter a description
Input Description
The first line is an integer N (1≤n≤16000). A total of n flowers on the original plant flower.
The second line has n integers, and the first integer represents the beautiful exponent of the I flower.
The next N-1 line is two integers A/b, indicating the presence of a branch of a flower and a B flower.
Output description
Output Description
A number that represents the maximum value of the sum of the "beautiful exponents" that can be obtained after a series of "trims". Guaranteed absolute value not exceeding
Over 2147483647.
Sample input
Sample Input
7
-1-1-1 1 1 1 0
1 4
2 5
3 6
6 U
5 7
6 7
Sample output
Sample Output
3
Data range and Tips
Data Size & Hint
Data range:
For 60% of data, ensure n≤1,000
For 100% of data, ensure n≤16,000
Tree-shaped DP
F[i]:=sigma (Max (0,f[son[i])) +init[i]
Code
Type Rec=record x,y:longint; End;var N:longint; I,j,k:longint; Max:longint; Init:array[1..16000]of Longint; Edge:array[1..32000]of Rec; Num1,num2:array[1..16000]of Longint; Used:array[1..16000]of Boolean; F:array[1..16000]of Longint; Num:longint; X,y:longint;procedure qsort (X,y:longint); var head,tail,k:longint; Temp:rec; Begin Head:=x; Tail:=y; k:=edge[(head+tail) div 2].x; While Head<tail does begin while Edge[head].x<k do Inc (head); While k<edge[tail].x do Dec (tail); If Head<=tail then begin Temp:=edge[head]; Edge[head]:=edge[tail]; Edge[tail]:=temp; Inc (head); Dec (tail);End End If Head<y then Qsort (head,y); If X<tail then Qsort (x,tail); End;procedure DFS (X:longint); var i,j,k:longint; Xxx:longint; Begin F[X]:=INIT[X]; xxx:=0; For I:=1 to Num2[x] does if USED[EDGE[NUM1[X]+I-1].Y] then begin k:=edge[num1[x]+i- 1].Y; Used[k]:=false; Inc (XXX); DFS (k); If F[k]>0 then f[x]:=f[x]+f[k]; End if (xxx=0) then BEGIN if (init[x]<0) then f[x]:=0 else f[x]:=init[x]; End End;begin READLN (n); num:=0; For I:=1 to n do read (Init[i]); For I:=1 to N-1 do Begin Inc (NUM); READLN (x, y); Edge[num].x:=x; Edge[num].y:=y; Inc. (NUM); Edge[num].x:=y; Edge[num].y:=x; End Qsort (1,num); Fillchar (Num1,sizeof (NUM1), 0); Fillchar (Num2,sizeof (num2), 0); For I:=1 to n do num1[i]:=maxlongint; For I:=num Downto 1 do BEGIN Inc (num2[edge[i].x]); If Num1[edge[i].x]>i then num1[edge[i].x]:=i; End Fillchar (used,sizeof (used), true); Fillchar (F,sizeof (f), 0); Used[i]:=false; DFS (i); MAX:=F[1]; For i:=2 to N does if max<f[i] then max:=f[i]; Writeln (max); end.
Noip Eve: Codevs, trimming flowers