Simulare-XI-12Martie | Warb

This was the problem page during the contest. Access the current page here.
Time limit: 3s Memory limit: 512MB Input: warb.in Output: warb.out

Task

Gigel, a renowned computer scientist (nicknamed the "shining star of Romanian computer science"), wants to color a beautiful tree using mm colors, encoded by natural numbers from 11 to mm.

After assigning each color cc a weight wcw_c and each node uu a value requreq_u, Gigel stated that a coloring of the tree nodes is good if each node uu meets one of the following conditions:

  1. Node uu has color 11; or
  2. If node uu has color c>1c > 1, then it has exactly requreq_u neighbors with color c1c - 1.

Gigel also defines the value of such a coloring as wc1+wc2++wcnw_{c_1} + w_{c_2} + \ldots + w_{c_n}, where cuc_u represents the color of node uu.

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 nn and mm — the number of nodes in the tree and the number of colors, respectively.
The second line contains mm natural numbers w1,w2,,wmw_1, w_2, \ldots, w_m — the weights of the mm colors.
The third line contains nn natural numbers req1,req2,,reqnreq_1, req_2, \ldots, req_n — the values associated with the nn nodes.
The next n1n-1 lines contain 22 natural numbers uu and vv, indicating that there is an edge in the tree between nodes uu and vv.

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

  • 1n1051 \leq n \leq 10^5
  • 1m1001 \leq m \leq 100
  • 1wi1041 \leq w_i \leq 10^4
  • 0reqi0 \leq req_i \leq degree of node ii
  • 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 n=1n=1
2 4 m=1m=1
3 22 n,m7n, m \le 7
4 10 reqi=0req_i=0
5 8 m10m \le 10, the tree is a chain (degrees of all nodes are less than or equal to 22)
6 7 the tree is a chain
7 10 the tree is a star (there is a node with degree n1n-1)
8 19 mm, degrees of all nodes 10\leq 10
9 6 reqi>0req_i > 0
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 c=[1,2,2]c = [1, 2, 2]:

  • Node 11 has color 11.
  • Node 22 has color 22 and exactly one neighbor with color c21=1c_2 - 1 = 1.
  • Node 33 has color 22 and exactly one neighbor with color c31=1c_3 - 1 = 1.
  • The value of this coloring is equal to wc1+wc2+wc3=w1+w2+w2=1+2+2=5w_{c_1} + w_{c_2} + w_{c_3} = w_1 + w_2 + w_2 = 1 + 2 + 2 = 5.

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 c=[2,1,1,2,2]c = [2, 1, 1, 2, 2]:

  • Node 11 has color 22 and exactly two neighbors with color c11=1c_1 - 1 = 1.
  • Node 22 has color 11.
  • Node 33 has color 11.
  • Node 44 has color 22 and exactly one neighbor with color c41=1c_4 - 1 = 1.
  • Node 55 has color 22 and exactly one neighbor with color c51=1c_5 - 1 = 1.
  • The value of this coloring is equal to wc1+wc2+wc3+wc4+wc5=2+1+1+2+2=8w_{c_1} + w_{c_2} + w_{c_3} + w_{c_4} + w_{c_5} = 2 + 1 + 1 + 2 + 2 = 8.

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 c=[2,1,1,3,2,2,2]c = [2, 1, 1, 3, 2, 2, 2].

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 c=[4,5,5,5,5,4]c = [4, 5, 5, 5, 5, 4].

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 c=[7,7,6,6,1,7,2]c=[7,7,6,6,1,7,2].

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 c=[2,2,4,2,5,2,1]c=[2,2,4,2,5,2,1].

Log in or sign up to be able to send submissions!