Heroes Never Die
Time limit2sMemory limit512 MB
Choose a subset of heroes to revive, gaining bond rewards when both endpoints are chosen, to maximize total reward minus revival cost.
- Level
Hard8 of 10
- Topics
- Graph, Minimum spanning tree, Greedy, Union-find
- Solved
- No attempts yet
Problem
Every hero has fallen, and Mercy says: "Heroes never die."
Reviving hero costs energy. Two heroes can be linked by a bond, and when both heroes of a bond are revived, that bond returns energy.
If Mercy revives heroes (), she gains the total energy returned by the bonds whose two heroes are both revived, minus the total energy spent on reviving them. Find the largest amount of energy Mercy can gain.
is a valid choice, so the answer is never negative.
Input
The first line contains the number of heroes () and the number of bonds (), separated by a space.
The second line contains , the energy needed to revive each hero.
Each of the next lines contains one bond as . and are distinct hero numbers between 1 and , and the same pair may appear in more than one bond. and are integers between 0 and 100.
Output
Print the largest amount of energy Mercy can gain, on one line.