Description
Background
The Stupid family learned that in the back garden of the HYC family, they walked three steps to the north, three steps to the west, three steps to the north, three steps to the east, and six steps to the north, 3 steps to the east, 12 steps to the south, and 2 steps to the West (--|) to find the entrance to the treasure, and the treasures are hidden in the mountains, must be dug out, so the Stupid family sent dogs to lead the miner's team to dig for treasures. (What do you think about the treasure of the HYC family when it is dug by a dog ?)
Description
To conceal the eyes and ears of the world, the makers of this treasure did not exit, but only the entrance. Many people who built the treasure died in it. now we know that there are N forks in the treasure, and there are treasures at the intersection. Each treasure point has a wealth value. dogs only carry M people, and for safety, at least one person must be left at each branch, this person can dig for a treasure (each person can only dig a local treasure) to ensure that he will not lose his way, and this maze has a unique feature. There is only one path between any two points. to make him happy, the dog decided to dig as many treasures as possible. now, the dog's fortinet computer is not around, and you can only ask for help. You need to know that the dog's lifelong happiness is in your hands .. (DOG ps: 00, you cannot abandon me like this ......)
Input Format
Row 3: two positive integers, N, and M. N, indicate the number of treasure points and M, indicate the number of people the dog carries (that is a lazy dog, he does not do anything on his own ). (N <= 1000, m <= 100)
Row 2nd: N integers. the I-th integer indicates the wealth of the I-th treasure. (guaranteed | wi | <maxint)
Row N + 2: two non-negative integers A and B, indicating that A leads to B. When A = 0, it indicates that A is the entry. (ensure that A, B <= n)
Output Format
Outputs the value of the treasure that dogs can bring back.
Sample Input
4 3
5 6
2 4
1 2
0 1
2 3
3 4
Sample output
13
Note: tree-based Dynamic Planning. For a multi-Cross Tree, we generally use a multi-Cross Tree to convert it into a binary tree to simplify the problem, that is, the left son and right brother. This is why f [x, k] = f [r [x], k].
1 var
2 l,r,a:array[0..1000] of longint;
3 f:array[0..1000,0..100] of longint;
4 i,j,k,m,n:longint;
5
6 function max(x,y:longint):longint;
7 begin
8 if x>y then exit(x);
9 exit(y);
10 end;
11
12 procedure dfs(x,k:longint);
13 var
14 i:longint;
15 begin
16 if x<=0 then exit;
17 if k<=0 then exit;
18 if f[x,k]>0 then exit;
19 dfs(r[x],k);
20 f[x,k]:=f[r[x],k];
21 for i:=0 to k-1 do
22 begin
23 dfs(l[x],i);
24 dfs(r[x],k-i-1);
25 f[x,k]:=max(f[x,k],f[l[x],i]+f[r[x],k-i-1]+a[x]);
26 end;
27 end;
28
29 begin
30 readln(n,m);
31 for i:=1 to n do read(a[i]); readln;
32 for i:=1 to n do
33 begin
34 readln(j,k);
35 r[k]:=l[j];
36 l[j]:=k;
37 end;
38 fillchar(f,sizeof(f),0);
39 dfs(l[0],m);
40 writeln(f[l[0],m]);
41 end.