Hide

Problem C
Hills in Gothenburg

As most students are aware, Gothenburg somehow seems to have more roads going uphill than downhill, regardless of which way you’re going. The knowledge of this made Lazy Smurf feel even more hopeless when his bicycle broke down in the middle of the city, on his way to Smurfette. While the bicycle could ride fine downhill, the pedals were broken and he had to walk with the bicycle on the uphill roads. Now Lazy Smurf needs your help finding the path to Smurfette which makes him have to walk uphill the least.

As everyone knows, all of Gothenburg can be represented as a graph 1: $N$ spots (numbered from $1$ to $N$), and $M$ roads (each road going directly between two spots). Each spot in the city has a known height $h_ i$, and if there is a direct road between spot $i$ and $j$, the height difference $h_ i-h_ j$ is the uphill distance to walk (if it is positive). To minimize the amount Lazy Smurf has to walk with the bicycle, we want to find the path from his location to Smurfette’s which minimizes the total height difference for all roads between their locations, counting downhill and flat roads as $0$.

Input

The first line contains two numbers $N$ and $M$ with a space between them. $N$ is the number of spots in the city, and $M$ is the number of roads. $1 \le N, M \le 10^5$

On the second row, there are two numbers separated by a space. The first number is the spot where Lazy Smurf is located, the second is the spot where Smurfette is located.

On the third row, there are $N$ numbers separated by spaces. The numbers $h_ i$ ($1 \le i \le N$) represent the height of the $i$th spot. It is always the case that $ -10^5 \le h_ i \le 10^5 $.

On each of the following $M$ rows, there will be two numbers $i$ and $j$, meaning that there is a road between spot $i$ and spot $j$. $i \ne j$, and every road will be specified exactly once.

Output

Output one number, the minimum total uphill distance Lazy Smurf has to travel to get to Smurfette.

Notes

In sample one, the best path is to go 1-2-4, which has a total uphill distance of $1+0=1$. If you instead took the path 1-3-4, the total uphill distance would have been $0+2=2$.

In sample two, 1-7-6-5 is one example of a path with the minimal upphill distance of $0+4+3=7$. Another possibility with uphill distance $7$ is 1-7-4-5. An example of a path with a different uphill distance is 1-2-3-4-5, having an uphill distance of $0+3+0+6 = 9$.

Samples

Sample Input 1 Sample Output 1
4 4
1 4
3 4 1 3
1 2
1 3
2 4
3 4
1
Sample Input 2 Sample Output 2
7 8
1 5
7 2 5 1 7 4 0
1 7
2 3
3 4
1 2
6 7
4 5
4 7
5 6
7

Please log in to submit a solution to this problem

Log in