Sum Tree

Time limit: 1s
Memory limit: 256MB
Input:
Output:

Task

You are given a tree with NN nodes. Each node has values assigned to it, value[i]value[i]. You must calculate the sum cost of every pair nodes (u,v)(u, v), where gcd(value[u],value[v])>1gcd(value[u], value[v]) > 1 and uuvv. For a pair of nodes (u,v)(u, v), we define the cost of that path to be the sum of value of all nodes that lie inside the (u,v)(u, v) path.

Input

The first line of the input will contain NN (1N100 0001 \leq N \leq 100 \ 000), the number of nodes. The second line will contain NN numbers, the ithi^{th} number, representing the value[i]value[i] (1value[i]30 0001 \leq value[i] \leq 30 \ 000). Each of the next N1N - 1 lines will contain a pair of nodes (u,v)(u, v), each representing an edge of the tree.

Restrictions

  • For tests worth 2020 points (1N1 0001 \leq N \leq 1 \ 000)
  • For tests worth 2020 more points, value[i]=value[1]value[i] = value[1] for i=1,2,...ni = 1, 2,...n

Output

The output will contain the sum of costs over all pairs of nodes (u,v)(u, v), such that gcd(value[u],value[v])>1gcd(value[u], value[v]) > 1

Example

stdin

5
2 7 14 22 77
1 2
1 3
2 4
4 5

stdout

442

Problem info

ID: 305

Editor: Dap

Author:

Source: IIOT 2022-23 Round I

Tags:

IIOT 2022-23 Round I

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