Nearly all of the Kingdom of Byteland is covered by forests and rivers. Small rivers meet to form bigger rivers, which also meet and, in the end, all the rivers flow together into one big river. The big river meets the sea near Bytetown.
There are
The king's accountants calculated how many trees are cut by each village per year. You must decide where to build the sawmills to minimize the total cost of transporting the trees per year. River transportation costs one cent per kilometre, per tree.
Task
Write a program that:
- reads from the standard input the number of villages, the number of additional sawmills to be built, the number of trees cut near each village, and descriptions of the rivers,
- calculates the minimal cost of river transportation after building additional sawmills,
- writes the result to the standard output.
Input
The first line of input contains two integers:
Each of the following
— the number of trees cut near village per year ( ), — the first village (or Bytetown) downriver from village ( ), — the distance (in kilometres) by river from village to ( ).
It is guaranteed that the total cost of floating all the trees to the sawmill in Bytetown in one year does not exceed
In
Output
The first and only line of the output should contain one integer: the minimal cost of river transportation (in cents).
Sample Input
4 2
1 0 1
1 1 10
10 2 5
1 2 3
Sample Output
4
Explanation for Sample Output
The above picture illustrates the example input data. Village numbers are given inside circles. Numbers below the circles represent the number of trees cut near villages. Numbers above the arrows represent rivers' lengths.
The sawmills should be built in villages
Comments