This page is still under construction.

Parts of this page are still being built. What you see may change.

Heroes Never Die

Time limit2sMemory limit512 MB

Summary
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 ii costs P(i)P(i) energy. Two heroes can be linked by a bond, and when both heroes of a bond are revived, that bond returns C(i)C(i) energy.

If Mercy revives KK heroes (0≤K≤N0 \le K \le N), 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.

K=0K = 0 is a valid choice, so the answer is never negative.

Input

The first line contains the number of heroes NN (1≤N≤50001 \le N \le 5000) and the number of bonds MM (0≤M≤500000 \le M \le 50000), separated by a space.

The second line contains P(1),P(2),…,P(N)P(1), P(2), \dots, P(N), the energy needed to revive each hero.

Each of the next MM lines contains one bond as A(i) B(i) C(i)A(i)\ B(i)\ C(i). A(i)A(i) and B(i)B(i) are distinct hero numbers between 1 and NN, and the same pair may appear in more than one bond. P(i)P(i) and C(i)C(i) are integers between 0 and 100.

Output

Print the largest amount of energy Mercy can gain, on one line.

Examples9

  1. Example 1

    Input
    5 5
    1 2 3 4 5
    1 2 3
    2 3 4
    1 3 3
    1 4 2
    4 5 3
    
    Expected output
    4
    
  2. Example 2

    Input
    1 0
    0
    
    Expected output
    0
    
  3. Example 3

    Input
    1 0
    100
    
    Expected output
    0
    
  4. Example 4

    Input
    2 1
    100 100
    1 2 100
    
    Expected output
    0
    
  5. Example 5

    Input
    2 1
    10 20
    1 2 100
    
    Expected output
    70
    
  6. Example 6

    Input
    3 3
    0 0 0
    1 2 0
    2 3 0
    1 3 0
    
    Expected output
    0
    
  7. Example 7

    Input
    4 3
    100 100 1 1
    1 2 100
    1 2 100
    3 4 5
    
    Expected output
    3
    
  8. Example 8

    Input
    6 6
    5 5 5 100 100 100
    1 2 6
    2 3 6
    1 3 6
    4 5 100
    5 6 100
    4 6 100
    
    Expected output
    3
    
  9. Example 9

    Input
    5 4
    0 0 0 0 0
    1 2 7
    2 3 8
    3 4 9
    4 5 10
    
    Expected output
    34