Power Supply
Time limit1sMemory limit512 MB
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 buildings, numbered from to . Each building produces and consumes a fixed amount of electricity. The value is the electricity produced by building minus the electricity it consumes, and it represents the building's "surplus power". If a building consumes more than it produces, is negative. No is 0, and every 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 power lines in total, numbered from to . The -th power line supplies units of power from building to building . 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 buildings so that the total surplus power of these buildings is maximized. Specifically, when is a subset of , that is, when Bob decides to buy the buildings in , the total surplus power of is defined as follows.
- (the total surplus power of the buildings in ) - (the total power that the buildings in supply through power lines to buildings not in )
For example, let , , , , , , , , , , .
- Building produces more electricity than it consumes, so its surplus power is , while the other two buildings have negative surplus power.
- Power line supplies unit of power from building to building .
- Power line supplies unit of power from building to building .
In this example there are ways for Bob to buy buildings, and the total surplus power for each case is as follows.
- If is the empty set: .
- If : .
- If : .
- If : .
- If : . Here, power line supplies power from building to building , but since Bob buys both buildings, it is not counted when computing according to the definition above.
- If : .
- If : .
- If : .
Among the ways, the total surplus power is maximized when .
As another example, let , , , , , , .
- If is the empty set: .
- If : .
- If : .
- If : .
Among the ways, the total surplus power is maximized when or when is the empty set.
Given , , , , , as input, find the maximum total surplus power Bob can achieve.
Input
The first line gives the number of test cases .
The first line of each test case gives and , separated by a space.
The second line gives the surplus power of each building, separated by spaces.
The following lines each give three integers , , . This means that units of power are being supplied from building to building .
Output
For each test case, print the maximum total surplus power Bob can achieve on its own line.
Constraints
- For every with , and .
- For every with , , , and .
- For every , with , there is no case where and at the same time. That is, all power lines are distinct when direction is taken into account.