Marie keeps the largest manager-closed team containing herself whose salaries span at most D.
Medium5TreeDFSIntervalsSortingNo attempts yetTime limit5sMemory limit512 MBFairland has strict laws about how a company organizes and pays its employees.
The government of Fairland has just passed one more law.
Marie is the CEO of Fairland General Stuff Corporation, and she has to make the company comply with the new law. That may mean laying off some employees. She knows the list of employees, the manager of each employee, and every salary. Find the largest number of employees she can keep, counting herself.
The first line contains the number of test cases T. T test cases follow.
The first line of each test case contains two space separated integers, the number of employees N and the largest allowed salary difference D. The second line contains four space separated integers S0, As, Cs, Rs, and the third line contains four space separated integers M0, Am, Cm, Rm. These eight integers define two sequences.
Marie's employee id is 0, and the other employees have ids from 1 to N−1. The salary of employee i is Si. For every employee i other than Marie, the manager is Mimodi. This means M0 does not decide Marie's manager. Marie has none.
For each test case, print one line in the form Case #x: y, where x is the test case number starting from 1 and y is the largest number of employees Marie can keep while laws 1 to 7 all hold. Marie herself counts toward that number.
In the first test case the company has only the CEO and no other employee. No law is broken, so nobody is laid off.
In the second test case the sequences for employees 1 through 5 are these.
The best choice is to keep employees 0, 1, and 5, whose salaries are 10, 13, and 8. Employee 2 cannot be kept, for example: her salary of 16 is more than 5 away from employee 0's salary of 10, and employee 0 cannot be laid off. When employee 2 leaves, every employee reporting to her leaves too.