Happyland can be described by a set of towns (numbered
to
) initially connected by
bidirectional roads (numbered
to
). Town
is the central town. It is guaranteed that one
can travel from town
to any other town through these roads. The roads are toll roads. A user
of the road
has to pay a toll fee of
cents to the owner of the road. It is known that all of
these
's are distinct. Recently,
additional new roads are completed and they are owned by a
billionaire Mr Greedy. Mr Greedy can decide the toll fees (not necessarily distinct) of the new
roads, and he has to announce the toll fees tomorrow.
Two weeks later, there will be a massive carnival in Happyland! Large number of participants
will travel to the central town and parade along the roads. A total of participants will
leave from town
and travel toward the central town. They will only travel on a set of selected
roads, and the selected roads will be announced a day before the event. By an old tradition, the
roads are to be selected by the richest person in Happyland, who is Mr Greedy. Constrained by
the same tradition, Mr Greedy must select a set of roads that minimizes the sum of toll fees in
the selected set and yet at the same time allow anyone to travel from town
to town
(hence,
the selected roads form a "minimum spanning tree" where the toll fees are the weights of the
corresponding edges). If there are multiple such sets of roads, Mr Greedy can select any set as
long as the sum is minimum.
Mr Greedy is well-aware that the revenue he received from the new roads does not solely
depend on the toll fees. The revenue from a road is actually the total fee collected from people
who travel along the road. More precisely, if
people travel along road
, the revenue from the
road
is the product
. Note that Mr Greedy can only collect fees from the new roads since
he does not own any of the old roads.
Mr Greedy has a sneaky plan. He plans to maximize his revenue during the carnival by
manipulating the toll fees and the roads selection. He wants to assign the toll fees to the new
roads (which are to be announced tomorrow), and select the roads for the carnival (which are to
be announced a day before the carnival), in such a way that maximizes his revenue from the
new roads. Note that Mr Greedy still has to follow the tradition of selecting a set of roads that
minimizes the sum of toll fees.
You are a reporter and want to expose his plan. To do so, you have to first write a program to determine how much revenue Mr Greedy can make with his sneaky plan.
Subtasks
Your program will be tested on 5 sets of instances as follows:
- (
points)
,
and
.
- (
points)
,
and
.
- (
points)
,
and
.
- (
points)
,
and
.
- (
points)
,
and
.
Input
Your program must read from the standard input. The first line contains three space-separated
integers ,
and
. The next
lines describe the initial
roads. The
-th of these lines
contains space-separated integers
,
and
, indicating that there is a bidirectional road between towns
and
with toll fee
. The next
lines describe the newly built
additional
roads. The
-th of these lines contains space-separated integers
and
,
indicating that there is a new road connecting towns
and
. The last line contains
space-separated integers, the
-th of which is
, the number of people from town
traveling to town
.
The input also satisfies the following constraints.
.
.
.
for each
and
.
, if
.
- Between any two towns, there is at most one road (including newly built ones).
Output
Your program must write to the standard output a single integer, which is the maximum total revenue obtainable.
Sample Input
5 5 1
3 5 2
1 2 3
2 3 5
2 4 4
4 3 6
1 3
10 20 30 40 50
Sample Output
400
Explanation for Sample Output
In this sample, Mr Greedy should set the toll fee of the new road to be
cents. With this
toll fee, he can select the roads
,
,
and
to minimize sum of toll fees, which
is
cents.
people from town
and
people from town
will pass through the new road
to town
and hence he can collect an optimal revenue of
cents.
If, on the other hand, the toll fee of the new road is set to be
cents. Now, constrained
by the tradition, Mr Greedy must select
,
,
and
as this is the only set that
minimizes the sum of toll fees. Hence, no revenue will be collected from the new road
during the carnival.
Comments