Under construction

Still being built. Things may change

Power Supply

Time limit1sMemory limit512 MB

Summary
Choose a subset of buildings to maximize their surplus power minus power sent along lines leaving the set.
Level

Medium7 of 10

Topics
Graph, Minimum spanning tree, Greedy, Math
Solved
No attempts yet

Problem

In the city of Alberta where Bob lives there are nn buildings, numbered from 11 to nn. Each building produces and consumes a fixed amount of electricity. The value viv_i is the electricity produced by building ii minus the electricity it consumes, and it represents the building's "surplus power". If a building consumes more than it produces, viv_i is negative. No viv_i is 0, and every viv_i is an integer.

Some buildings are connected to each other by power lines, so part of the electricity produced by one building is sent to another building. There are mm power lines in total, numbered from 11 to mm. The jj-th power line supplies zj>0z_j > 0 units of power from building xjx_j to building yjy_j. Each power line has a direction, and between two buildings there is at most one power line in the same direction.

Since electricity has become a very important resource, Bob wants to buy some of the nn buildings so that the total surplus power of these buildings is maximized. Specifically, when SS is a subset of {1,2,…,n}\{1, 2, \dots, n\}, that is, when Bob decides to buy the buildings in SS, the total surplus power V(S)V(S) of SS is defined as follows.

  • (the total surplus power of the buildings in SS) - (the total power that the buildings in SS supply through power lines to buildings not in SS)

For example, let n=3n = 3, m=2m = 2, v1=4v_1 = 4, v2=−1v_2 = -1, v3=−2v_3 = -2, x1=1x_1 = 1, x2=2x_2 = 2, y1=2y_1 = 2, y2=3y_2 = 3, z1=1z_1 = 1, z2=1z_2 = 1.

  • Building 11 produces more electricity than it consumes, so its surplus power is 44, while the other two buildings have negative surplus power.
  • Power line 11 supplies 11 unit of power from building 11 to building 22.
  • Power line 22 supplies 11 unit of power from building 22 to building 33.

In this example there are 23=82^3 = 8 ways for Bob to buy buildings, and the total surplus power for each case is as follows.

  • If SS is the empty set: V(S)=0V(S) = 0.
  • If S={1}S = \{1\}: V(S)=4−1=3V(S) = 4 - 1 = 3.
  • If S={2}S = \{2\}: V(S)=(−1)−1=−2V(S) = (-1) - 1 = -2.
  • If S={3}S = \{3\}: V(S)=(−2)−0=−2V(S) = (-2) - 0 = -2.
  • If S={1,2}S = \{1, 2\}: V(S)=(4−1)−1=2V(S) = (4-1) - 1 = 2. Here, power line 11 supplies power from building 11 to building 22, but since Bob buys both buildings, it is not counted when computing V(S)V(S) according to the definition above.
  • If S={2,3}S = \{2, 3\}: V(S)=(−1−2)−0=−3V(S) = (-1-2) - 0 = -3.
  • If S={1,3}S = \{1, 3\}: V(S)=(4−2)−1=1V(S) = (4-2) - 1 = 1.
  • If S={1,2,3}S = \{1, 2, 3\}: V(S)=(4−1−2)−0=1V(S) = (4-1-2) - 0 = 1.

Among the 88 ways, the total surplus power is maximized when S={1}S = \{1\}.

As another example, let n=2n = 2, m=1m = 1, v1=1v_1 = 1, v2=−5v_2 = -5, x1=1x_1 = 1, y1=2y_1 = 2, z1=1z_1 = 1.

  • If SS is the empty set: V(S)=0V(S) = 0.
  • If S={1}S = \{1\}: V(S)=1−1=0V(S) = 1 - 1 = 0.
  • If S={2}S = \{2\}: V(S)=−5V(S) = -5.
  • If S={1,2}S = \{1, 2\}: V(S)=(1−5)−0=−4V(S) = (1-5) - 0 = -4.

Among the 44 ways, the total surplus power is maximized when S={1}S = \{1\} or when SS is the empty set.

Given nn, mm, vv, xx, yy, zz as input, find the maximum total surplus power Bob can achieve.

Input

The first line gives the number of test cases TT.

The first line of each test case gives nn and mm, separated by a space.

The second line gives the surplus power viv_i of each building, separated by spaces.

The following mm lines each give three integers xjx_j, yjy_j, zjz_j. This means that zjz_j units of power are being supplied from building xjx_j to building yjy_j.

Output

For each test case, print the maximum total surplus power Bob can achieve on its own line.

Constraints

  • For every ii with 1≤i≤n1 ≤ i ≤ n, −100 000≤vi≤100 000-100\,000 ≤ v_i ≤ 100\,000 and vi≠0v_i \ne 0.
  • For every jj with 1≤j≤m1 ≤ j ≤ m, xj≠yjx_j \ne y_j, 1≤xj,yj≤n1 ≤ x_j, y_j ≤ n, and 1≤zj≤100 0001 ≤ z_j ≤ 100\,000.
  • For every jj, kk with 1≤j<k≤m1 ≤ j < k ≤ m, there is no case where xj=xkx_j = x_k and yj=yky_j = y_k at the same time. That is, all power lines are distinct when direction is taken into account.

Examples1

  1. Example 1

    Input
    6
    2 1
    1 -2
    1 2 3
    2 1
    1 -2
    2 1 3
    3 2
    4 -1 -2
    1 2 1
    2 3 1
    3 6
    10 10 10
    1 2 2
    2 1 3
    1 3 4
    3 1 5
    2 3 6
    3 2 7
    2 1
    1 -5
    1 2 1
    3 2
    1 1 -2
    1 3 1
    2 3 1
    
    Expected output
    0
    1
    3
    30
    0
    0