#GL0000382. Tối đa hóa giá trị tại gốc (Maximizing Root)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tối đa hóa giá trị tại gốc (Maximizing Root)
Maximizing Root
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You have a tree of vertices rooted at vertex . Vertex initially has a positive value . You may perform at most operations:
- Select a vertex that has not been selected before.
- Choose a positive integer dividing the current values of all vertices in the subtree rooted at .
- Multiply all values in that subtree by .
A vertex may be selected directly at most once, but an ancestor's operation may affect it again. A subtree includes its root. Maximize the final value of vertex .
PHOJ input convention: Each input file contains a single tree with no leading test count .
Input
The first line contains . The second line contains the vertex values. The following lines contain undirected edges forming a tree.
Output
Print the maximum achievable final value of vertex .
Subtasks
-
Subtask 1 (20 points): , , ; valid tree.
-
Subtask 2 (30 points): , , ; valid tree.
-
Subtask 3 (50 points): , , ; valid tree.
Examples
Example 1
Input
5 2
24 12 24 6 12
1 2
1 3
2 4
2 5
Output
288
Explanation
First select vertex with multiplier ; the values become . Now all values are multiples of , so select the root with multiplier , giving root value . This is optimal within two operations.
Example 2
Input
5 3
24 12 24 6 12
1 2
1 3
2 4
2 5
Output
576
Explanation
Select vertex with multiplier , then vertex with multiplier , and finally the root with multiplier . The root becomes .