Logo
(追記) (追記ここまで)

25229번 - GCD Harmony 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
4 초 (추가 시간 없음) 1024 MB5921097724.759%

문제

Consider a tree with undirected edges, where each node has an integer value. Adjacent nodes are said to be GCD-harmonic if the greatest common divisor (GCD) of their values is strictly greater than 1ドル$.

You can modify the value of any tree node to any positive integer. The cost of this operation is equal to the new node value, regardless of the node's original value. You can change as many node values as needed, and node values do not need to be unique.

What is the minimum total cost to make every pair of adjacent nodes in the tree GCD-harmonic?

입력

The first line of input contains a single integer $n$ (2ドル \leq n \leq 5,000円$), which is the number of nodes in the tree. Tree nodes are numbered from 1ドル$ to $n$.

Each of the next $n$ lines contains an integer $v$ (1ドル \le v \le 100$). These are the initial values of the nodes (which are not guaranteed to be unique), in node number order.

Each of the next $n - 1$ lines contains two integers $a$ and $b$ (1ドル \leq a, b \leq n, a \neq b$), indicating a tree edge between nodes $a$ and $b$. It is guaranteed that these edges form a tree.

출력

Output a single integer, which is the minimum total cost to make every pair of adjacent nodes in the tree GCD-harmonic.

제한

예제 입력 1

6
5
6
3
4
9
12
1 2
1 3
1 4
1 6
3 5

예제 출력 1

6

예제 입력 2

3
1
2
3
3 1
2 3

예제 출력 2

4

힌트

출처

ICPC > Regionals > North America > North America Championship > North America Championship 2022 E번

Camp > Petrozavodsk Programming Camp > Summer 2022 > Day 1: Welcome Contest D번

  • 문제를 만든 사람: Bowen Yu
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /