Farmer John has given Bessie free reign over one of his best fields to eat grass. The field is covered in N patches of grass (1 <= N <= 1000), each with a distinct quality value. If Bessie eats grass of quality Q, she gains Q units of energy. Each patch is connected to up to 10 neighboring patches via bi-directional paths, and it takes Bessie E units of energy to move between adjacent patches (1 <= E <= 1,000,000). Bessie can start grazing in any patch she wishes.
Bessie is picky: once she eats grass of a certain quality, she'll never eat grass at or below that quality level again. She is still happy to walk through patches without eating their grass. Please help determine the maximum amount of energy Bessie can accumulate.
Input
Line 1: N and E. Each of the remaining N lines describes a patch with two integers Q and D giving the quality of the patch (1...1,000,000) and its number of neighbors, followed by the D neighbor indices.
Output
The maximum amount of energy Bessie can accumulate.
Example 1
5 2 4 1 2 1 3 1 3 4 6 2 2 5 5 2 2 5 2 2 3 4
7
Output details: Bessie starts at patch 4 (gain 5), walks to 5 (-2), walks to 3 (-2), eats at 3 (gain 6); total 7.
Discussion
Threads about this problem. Spoilers are collapsed; wrap hints in
||double bars||.No discussion yet. Start a thread if something here needs explaining.