Miscellaneous [II]
Treetown is a town of various suburbs connected by main roads. The roads have been constructed in such a way that:
- Every suburb can be reached by taking these roads from suburb to suburb
- Between any two suburbs, there is only a single path connecting the two
- All roads are bidirectional
The nature of this construction means that a single road failure leads to a disconnection of the town, causing major delays. You'd like to analyse the possible impact of removing a road from Treetown.
Every road in Treetown has a distance . For any pair of suburbs in Treetown, we compute the contribution to the Treetown economy as
Where is the collection of roads on the path from to . For example, take the following graph, with 4 suburbs, and 3 roads from to , to and to , of lengths 1, 3 and 4.
Then for the suburb pairing and , the contribution to productivity is
summing the roads from to and to .
In this problem, we want you to assess the reduction in productivity after removing a single road (in other words, summing the productivity contribution of suburb pairings that are connected via the removed road.)
For example, removing the edge to would remove pairings , , , , which contribute productivity.
Input
Input will begin with a single integer , representing the number of suburbs. lines will follow. Each line will contain 3 space separated integers, and . This means there is a road from to of distance .
Finally, a single integer will follow. This represents a (1-indexed) index of one of the roads just inputted.
Output
Output should contain a single integer - the hit to productivity removing road would cause.
Constraints
Example
Input
Taking the example in the statement, this can be written as:
4
1 2 1
2 3 3
3 4 4
2
Output
For which the output should be
22
as discussed previously.
Comments