Woburn Challenge 2015-16 Round 1 - Senior Division
It's no surprise that the Programming Enrichment Group at Woburn participates in a wide number of programming contests. However, not all contests are held at the same place (worry not – the Woburn Challenge finals will be held at a single location). In fact if too many people register for a contest, the contest organizers may even demand that the same contest be held in two entirely different towns! This will make it a lot harder for our young Woburnites to compete in the contest, but certainly won't stop them from trying.
There are towns uniquely numbered with integers from to , and one-way roads running amongst them. Specifically, the -th road (for ) allows one to travel from a town to a different town and has a distance of kilometres. No two roads run between the same pair of towns in the same direction. A very large programming contest is soon to be held across two locations, with the main contest site located in town and the secondary contest site located in town . A non-zero number of Woburnites will be competing in this contest, with of them living in town (for each ). Each competitor must select one of the two contest sites and travel to it using a sequence of roads. This sequence is possibly empty, if they're fortunate enough to live in the same town as their chosen contest site.
There is a catch! Resources are limited at the second contest site, so the contest organizers have insisted that no more than of the competitors attend the secondary site (located in town ). Given that the Woburnites collaborate to come up with the best travel strategy, you must help them determine the minimum total combined distance that they must travel in order to all attend the contest, such that no more than of them travel to the secondary site.
In test cases worth 60% of the marks, .
In a subset of those cases worth 30% of the marks, (for
).
Input Specification
Line of input will contain three space-separated integers, the values
of , , and , respectively representing the number of towns,
roads, and the maximum number of Woburnites that may attend the second
site.
There will be lines to follow. The -th of these lines (for
) will contain a single integer , representing the
number of Woburnites living in town .
There will be lines to follow. The -th of these lines (for
) will contain three space-separated integers, the values of
, , and representing the -th one-way road.
Output Specification
The output should consist of a single integer. If it is possible for the
Woburnites to travel to all contest sites with no more than of them
attending the secondary site, then output the minimum total combined
distance that they must travel to do so. Output -1
if it is impossible
for all of them to reach a contest site while satisfying the condition.
Please note that the answer may not necessarily fit in a 32-bit integer.
Sample Input
4 5 5
2
1
5
7
1 2 1
3 2 1
3 4 1
4 1 1
4 3 1
Sample Output
13
Explanation
There are towns and roads between them. At most people may attend the second contest site. Each road happens to have a distance of . An optimal travel strategy is as follows.
- We let competitors from towns and attend the contest sites at their respective towns (incurring an additional kilometres to our total distance).
- We let all of our competitors from town travel to the main contest site (incurring a total of kilometres).
- We let of the competitors from town travel to the secondary contest site (incurring a total of kilometres).
- We let the remaining competitor from town must travel to the main site through town (incurring an additional kilometres to our total distance).
The total distance traveled across all of the competitors is kilometres.
Comments