atcoder#ABC237E. [ABC237E] Skiing
[ABC237E] Skiing
Score : points
Problem Statement
AtCoder Ski Area has open spaces called Space , Space , , Space . The altitude of Space is . There are slopes that connect two spaces bidirectionally. The -th slope connects Space and Space . It is possible to travel between any two spaces using some slopes.
Takahashi can only travel between spaces by using slopes. Each time he goes through a slope, his happiness changes. Specifically, when he goes from Space to Space by using the slope that directly connects them, his happiness changes as follows.
- If the altitude of Space is strictly higher than that of Space , the happiness increases by their difference: .
- If the altitude of Space is strictly lower than that of Space , the happiness decreases by their difference multiplied by : .
- If the altitude of Space is equal to that of Space , the happiness does not change.
The happiness may be a negative value.
Initially, Takahashi is in Space , and his happiness is . Find his maximum possible happiness after going through any number of slopes (possibly zero), ending in any space.
Constraints
- $N-1 \leq M \leq \min( 2\times 10^5,\frac{N(N-1)}{2})$
- if .
- All values in input are integers.
- It is possible to travel between any two spaces using some slopes.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
4 4
10 8 12 5
1 2
1 3
2 3
3 4
3
If Takahashi takes the route Space Space Space , his happiness changes as follows.
- When going from Space (altitude ) to Space (altitude ), it decreases by and becomes .
- When going from Space (altitude ) to Space (altitude ), it increases by and becomes .
If he ends the travel here, the final happiness will be , which is the maximum possible value.
2 1
0 10
1 2
0
His happiness is maximized by not moving at all.