CF1778F.Maximizing Root

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a rooted tree consisting of nn vertices numbered from 11 to nn . Vertex 11 is the root of the tree. Each vertex has an integer value. The value of ii -th vertex is aia_i . You can do the following operation at most kk times.

  • Choose a vertex vv that has not been chosen before and an integer xx such that xx is a common divisor of the values of all vertices of the subtree of vv . Multiply by xx the value of each vertex in the subtree of vv .

What is the maximum possible value of the root node 11 after at most kk operations? Formally, you have to maximize the value of a1a_1 .

A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. The subtree of a node uu is the set of all nodes yy such that the simple path from yy to the root passes through uu . Note that uu is in the subtree of uu .

输入格式

The first line contains an integer tt ( 1t500001 \leq t \leq 50\,000 ) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and kk ( 2n1052 \leq n \leq 10^5 , 0kn0 \leq k \leq n ) — the number of vertices in the tree and the number of operations.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n ( 1ai10001 \leq a_i \leq 1000 ), where aia_i denotes the value of vertex ii .

Each of the next n1n - 1 lines contains two integers uiu_i and viv_i ( 1ui,vin1 \leq u_i, v_i \leq n , uiviu_i \neq v_i ), denoting the edge of the tree between vertices uiu_i and viv_i . It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5 .

输出格式

For each test case, output the maximum value of the root after performing at most kk operations.

输入输出样例

  • 输入#1

    2
    5 2
    24 12 24 6 12
    1 2
    1 3
    2 4
    2 5
    5 3
    24 12 24 6 12
    1 2
    1 3
    2 4
    2 5

    输出#1

    288
    576

说明/提示

Both examples have the same tree:

For the first test case, you can do two operations as follows:

  • Choose the subtree of vertex 44 and x=2x = 2 . After this operation, the node values become {24,12,24,12,12}.\{24, 12, 24, 12, 12\}.
  • Choose the subtree of vertex 11 and x=12x = 12 . After this operation, the node values become {288,144,288,144,144}.\{288, 144, 288, 144, 144\}.

The value of the root is 288288 and it is the maximum.For the second test case, you can do three operations as follows:

  • Choose the subtree of vertex 44 and x=2x = 2 . After this operation, the node values become {24,12,24,12,12}.\{24, 12, 24, 12, 12\}.
  • Choose the subtree of vertex 22 and x=4x = 4 . After this operation, the node values become {24,48,24,48,48}.\{24, 48, 24, 48, 48\}.
  • Choose the subtree of vertex 11 and x=24x = 24 . After this operation, the node values become {576,1152,576,1152,1152}.\{576, 1152, 576, 1152, 1152\}.

The value of the root is 576576 and it is the maximum.

首页