n sections numbered from 1. Section j still needs work[j - 1] units of work, and every section has to be signed off by an inspector.
The inspector follows the schedule visits: at second s (from 1 to m) they stand at section visits[s - 1]. Every second, the crew does exactly one of three things:
- does one unit of work on any section;
- gets the section where the inspector stands signed off, provided it has no work left;
- does nothing.
Return the earliest second by the end of which every section can be signed off, or -1 if it cannot be done within m seconds.work=[1, 0], visits=[2, 1, 2, 1] → 3
Second 1: work on site 1, finishing it. Second 2: the inspector is at site 1 and signs it off. Second 3: the inspector is at site 2, which needed no work, and signs it off. The answer is 3.
work=[2, 1], visits=[1, 2, 2, 1, 1] → 5
There are 3 seconds of work and 2 sign-offs. Site 2 can be signed off at second 3 and site 1 at second 5, so everything fits in exactly 5 seconds.
work=[0, 0, 1], visits=[1, 2, 1, 2] → -1
The inspector never visits site 3, so not everything can be signed off.
work=[1, 0], visits=[2, 1, 2, 1]
3
Run checks the first 3 tests in your browser. Submit checks all 116 tests on the server, hidden ones included.