WC '15 Contest 1 S4 - Chocolate Milk
View as PDFWoburn Challenge 2015-16 Round 1 - Senior Division

After the finals of the 2015-16 Woburn Challenge, young programmers from all over the region will be invited to Woburn for a top-secret after-party (not really though)! The school has generously agreed to nourish them with copious amounts of chocolate milk.
To this end, the Student Activity Council (SAC) has acquired 
 cisterns with infinite capacities, and positioned them at
distinct heights off the ground. The cisterns are uniquely numbered from
 to 
 in order of their heights from lowest to highest. For each
cistern 
 (such that 
), there will be 
 litres of chocolate milk pumped into it per second,
directly from Scarborough's local chocolate milk farm. Additionally,
there will be a pipe leading down from each cistern 
 to a different
cistern 
. According to the laws of gravity, chocolate milk can
only flow from a higher cistern to a lower cistern, so it's guaranteed
that 
. The pipe coming out of 
 will be wide enough to
allow at most 
 
 litres of chocolate
milk to flow through it per second. Chocolate milk flows through the
system as one might expect – for every cistern, the amount of chocolate
milk flowing out of it cannot exceed the total amount flowing into it
(both directly from the farm and down from the other cisterns).
However, PEG has received enough funding this year to be able to upgrade
 
 of the 
 pipes. When a pipe is upgraded, it
permanently becomes able to support an infinite amount of chocolate milk
flowing through it.
Cistern  is located in Woburn's cafeteria, and the students will have
permission to drink from it at will! Coding is a strenuous sport, so we
know that everyone will be extremely thirsty. So the more chocolate milk
flows into that cistern, the more they will collectively drink. And the
more chocolate milk they consume, the more their coding skills will
improve for next year's Challenge. Given the configuration of cisterns
and pipes, and provided that we choose the optimal set of 
 pipes to
upgrade, what's the maximum rate of incoming chocolate milk flow (in
litres per second) that cistern 
 can attain?
In test cases worth 25% of the marks, .
In a subset of those test cases worth 10% of the marks, .
Input Specification
Line 1 of input will contain two space-separated integers  and 
,
respectively representing the number of cisterns and the number of
upgrades allowed.
Line 2 to  of input will each contain information about a cistern.
Specifically, line 
 of input (for 
) will contain three
space-separated integers 
, 
, and 
, respectively
representing the rate at which chocolate milk is being pumped into
cistern 
 (in litres per second), the cistern that the pipe from
cistern 
 flows into, and the maximum rate at which chocolate milk (in
litres per second) can flow through this pipe.
Output Specification
Output a single integer, the maximum rate of incoming chocolate milk
flow that cistern  can attain, in litres per second.
Sample Input
5 2
20 1 50
20 1 30
20 2 5
40 2 30
Sample Output
90
Explanation
There are  cisterns and we're allowed to upgrade 
 of the pipes. The
network of cisterns and pipes is depicted in the following diagrams. The
numbers on the pipes represent the maximum rates of chocolate milk flow,
and the bottom numbers on the cisterns represent the rate at which
chocolate milk is being pumped into them. The left diagram depicts the
original configuration, and the right diagram depicts the system after
pipes have been upgraded.

By upgrading the pipes flowing down from cisterns  and 
, cistern 
 in
the cafeteria is able to receive 
 litres/second from cistern 
, 
litres/second from cistern 
 directly, and 
 litres/second from cistern
 indirectly (
 of which came from cistern 
 and 
 of which came from
cistern 
). In total, this allows cistern 
 to receive 
litres/second.
Comments