k weeks moving between n offices in different cities (numbered 0 to n - 1). On the Monday of the first week the employee is at office 0.
The n × n matrix routes describes flights: routes[i][j] = 1 if there is a direct flight from city i to city j, otherwise 0 (no city has a flight to itself, and a return flight is not guaranteed). Flights are allowed only on Monday morning, at most one per Monday, or the employee can stay put. A flight takes no time.
The n × k matrix rest says rest[i][w] is how many days the employee can rest in city i during week w. Rest counts toward the city where the week is spent (after a Monday flight, the whole week counts for the new city). The other days of the week are work days.
Return the largest total number of rest days over the k weeks.routes=[[0, 1, 0], [0, 0, 1], [1, 0, 0]], rest=[[2, 1, 0], [5, 0, 4], [0, 6, 7]] → 18
Week 1: fly to office 1 and rest 5 days. Week 2: fly to office 2 and rest 6. Week 3: stay there for another 7. That is 18 in total.
routes=[[0, 0], [0, 0]], rest=[[3, 2], [7, 7]] → 5
There are no flights, so every week is spent in office 0: 3 + 2 = 5.
routes=[[0, 1, 0], [0, 0, 1], [1, 0, 0]], rest=[[2, 1, 0], [5, 0, 4], [0, 6, 7]]
18
Run checks the first 3 tests in your browser. Submit checks all 63 tests on the server, hidden ones included.