#K8816. Magical Energy Calculation
Magical Energy Calculation
Magical Energy Calculation
You are given a forest of mystical animals, each endowed with a magical energy level. In addition, a number of magical pathways are provided, each connecting a pair of animals. Your task is to calculate the total magical energy of the forest, which is defined as the sum of the absolute differences in energy levels for each connected pair.
Formally, if the magical energy levels are given as an array E = [E1, E2, ..., Em] and the pathways are given as pairs \( (u,v) \), then the total magical energy \( T \) is defined by:
[ T = \sum_{(u,v) \in P} \left| E_u - E_v \right| ]
It is guaranteed that the indices provided in the pathways are 1-indexed. Use standard input and output for reading the input data and printing the result.
Input constraints and details are provided below.
inputFormat
The input is given via standard input with the following format:
- The first line contains two integers \( m \) and \( e \), where \( m \) is the number of mystical animals and \( e \) is the number of magical pathways.
- The second line contains \( m \) integers representing the magical energy levels of each animal.
- The following \( e \) lines each contain two integers \( u \) and \( v \), indicating that there is a pathway connecting the \( u^{th} \) and \( v^{th} \) animal.
outputFormat
Output a single integer representing the total magical energy, which is the sum of the absolute differences of the magical energy levels for all the given pathways.
## sample4 3
10 20 30 40
1 2
2 3
3 4
30