#K65282. Largest Connected Component Weight

    ID: 32163 Type: Default 1000ms 256MiB

Largest Connected Component Weight

Largest Connected Component Weight

You are given n boxes, where the i-th box has a weight w_i. Additionally, you are provided with m pairs of integers, where each pair (u, v) indicates that the box u is connected to box v. Two boxes are in the same connected component if and only if there is a path connecting them directly or indirectly. Your task is to determine the maximum total weight of any connected component.

Input Format: The input is read from standard input in the following format:

  • The first line contains an integer n, the number of boxes.
  • The second line contains n space-separated integers representing the weights of the boxes: \(w_1, w_2, \dots, w_n\).
  • The third line contains an integer m, the number of connections.
  • The next m lines each contain two space-separated integers u and v, indicating that box u is connected to box v. (Boxes are indexed from 0.)

Output Format: Print a single integer representing the weight of the heaviest connected component.

Example:

Input:
4
1 2 3 4
2
0 1
2 3

Output:
7

Note: If there are no connections, each box is its own component.

inputFormat

The input is given via standard input (stdin) and consists of multiple lines. The structure of the input is as follows:

  • The first line contains an integer n (1 ≤ n ≤ 105), the number of boxes.
  • The second line contains n space-separated integers, the weights of the boxes.
  • The third line contains an integer m, the number of connections.
  • The following m lines each contain two integers u and v (0 ≤ u, v < n), indicating a connection between box u and box v.

outputFormat

Output a single integer representing the total weight of the largest connected component among the boxes. The output should be written to standard output (stdout).

## sample
4
1 2 3 4
2
0 1
2 3
7