Toll
Time limit3sMemory limit128 MB
A billionaire sets tolls on K new roads of his choosing so that the minimum spanning tree routing all traffic to town 1 maximizes his revenue, where K is at most 20.
- Level
Hard8 of 10
- Topics
- Minimum spanning tree, Greedy, Tree, Dynamic programming
- Solved
- No attempts yet
Problem
Happyland is a country of towns numbered to . Town is the capital. Initially the towns are connected by two-way roads numbered to , and it is guaranteed that every town can reach town using these roads. Every road is a toll road: using road costs cents, paid to that road's owner. All are distinct.
Recently a billionaire, Mr. Greedy, finished building additional new roads, all of which he owns. He may set the toll of each new road to any positive integer he likes (the new tolls need not be distinct), and he must announce these tolls tomorrow.
Two weeks from now a huge carnival will take place. For each town , exactly people will start at town and travel to the capital, town . They may only use a set of selected roads, which is announced the day before the carnival. By tradition, the selected roads are chosen by the richest person in Happyland, Mr. Greedy. The same tradition requires the selected set to (a) still let everyone travel from every town to town , and (b) have the minimum possible total toll among all such sets. In other words, the selected roads must form a minimum spanning tree, using the tolls as edge weights. When several sets tie for the minimum total, Mr. Greedy may pick any one of them.
Mr. Greedy earns money only from the new roads (he owns none of the old ones). The revenue from a road equals its toll multiplied by the number of people who walk along it: if road has toll and people use it, its revenue is .
Mr. Greedy wants to maximize his total revenue from the new roads by cleverly choosing the new tolls and, when the minimum-total set is not unique, by cleverly choosing the selected roads, while still obeying the tradition of minimum total toll. Determine the maximum total revenue he can obtain.
Input
The first line contains three integers , , and .
Each of the next lines contains three integers , , and : old road connects towns and and has toll .
Each of the next lines contains two integers and : new road connects towns and .
The last line contains integers , where is the number of people starting from town .
Constraints:
- All are distinct.
- Between any two towns there is at most one road (counting both old and new roads).
- Using only the old roads, every town can reach town .
Output
Print a single integer: the maximum total revenue Mr. Greedy can obtain.
Hint

In the situation shown above, Mr. Greedy should set the toll of the new road to . With this toll he can select the roads , , , and , whose total toll is the minimum possible, . The people from town and the people from town then cross the new road on their way to town , so its revenue is .
If instead he set the toll of to , the tradition would force him to select , , , and , the only minimum-total set, and no one would use the new road, earning him nothing.