#C1391. Maximum Magical Energy

    ID: 43500 Type: Default 1000ms 256MiB

Maximum Magical Energy

Maximum Magical Energy

You are given a forest consisting of n trees. Each tree has an associated magical energy value. Additionally, you are provided with m merge operations, where each operation connects two trees (or their already merged groups). When trees are merged, the magical energy of the group becomes the sum of the energies of all the trees in that connected component.

Your task is to calculate the sum of the magical energies of all connected components after performing all merge operations. If no merge operation is performed, each tree stands alone, and the total energy is simply the sum of individual tree energies.

Note: The merge operations are given in a 1-indexed format.

inputFormat

The input is given via standard input (stdin) and has the following format:

The first line contains two integers, n and m, where n is the number of trees and m is the number of merge operations.

The second line contains n space-separated integers representing the magical energy values of the trees.

Then m lines follow, each containing two integers a and b (1-indexed) representing a merge operation between tree a and tree b.

outputFormat

Output a single integer to standard output (stdout): the sum of magical energies of the resulting connected components after performing all provided merge operations.## sample

5 4
10 20 30 40 50
1 2
1 3
3 4
4 5
150