Task
Gigel, a renowned computer scientist (nicknamed the "shining star of Romanian computer science"), wants to color a beautiful tree using colors, encoded by natural numbers from to .
After assigning each color a weight and each node a value , Gigel stated that a coloring of the tree nodes is good if each node meets one of the following conditions:
- Node has color ; or
- If node has color , then it has exactly neighbors with color .
Gigel also defines the value of such a coloring as , where represents the color of node .
Help Gigel determine the highest value of a good coloring!
Input data
The first line of the input file warb.in
contains two natural numbers and — the number of nodes in the tree and the number of colors, respectively.
The second line contains natural numbers — the weights of the colors.
The third line contains natural numbers — the values associated with the nodes.
The next lines contain natural numbers and , indicating that there is an edge in the tree between nodes and .
Output data
On the first line of the output file warb.out
, print a single natural number, the highest value of a good coloring.
Constraints and clarifications
- degree of node
- It is guaranteed that the graph in the input file is a tree (a connected acyclic undirected graph).
- To obtain points for a particular subtask, at least one submitted source code will have to pass all the tests of that subtask.
# | Points | Constraints |
---|---|---|
0 | 0 | Example |
1 | 4 | |
2 | 4 | |
3 | 22 | |
4 | 10 | |
5 | 8 | , the tree is a chain (degrees of all nodes are less than or equal to ) |
6 | 7 | the tree is a chain |
7 | 10 | the tree is a star (there is a node with degree ) |
8 | 19 | , degrees of all nodes |
9 | 6 | |
10 | 10 | No additional constraints |
Example 1
warb.in
3 2
1 2
1 1 1
1 2
1 3
warb.out
5
Explanation
For the first example, an optimal coloring is :
- Node has color .
- Node has color and exactly one neighbor with color .
- Node has color and exactly one neighbor with color .
- The value of this coloring is equal to .
Example 2
warb.in
5 4
1 2 3 4
2 3 1 1 1
1 2
1 3
2 4
2 5
warb.out
8
Explanation
For the second example, an optimal coloring is :
- Node has color and exactly two neighbors with color .
- Node has color .
- Node has color .
- Node has color and exactly one neighbor with color .
- Node has color and exactly one neighbor with color .
- The value of this coloring is equal to .
Example 3
warb.in
7 4
1 9 8 7
2 2 0 0 1 1 1
1 2
1 3
2 4
2 5
3 6
3 7
warb.out
46
Explanation
For the third example, an optimal coloring is .
Example 4
warb.in
6 5
7 9 50 30 80
0 1 0 0 1 0
1 2
2 3
3 4
3 5
5 6
warb.out
380
Explanation
For the fourth example, an optimal coloring is .
Example 5
warb.in
7 7
458 2960 8526 2565 6679 9621 8814
1 0 0 0 2 1 1
3 4
1 2
1 4
5 7
4 5
4 6
warb.out
49102
Explanation
For the fifth example, an optimal coloring is .
Example 6
warb.in
7 7
3564 9408 8285 6312 7323 3223 4441
0 0 0 1 1 1 2
3 2
5 3
4 1
7 3
4 7
6 7
warb.out
54831
Explanation
For the sixth example, an optimal coloring is .