n tanks: tank i holds levels[i] litres right now, and rates[i] litres flow into it every second.
Each second goes like this: first water flows into all tanks at once, then you may pick one tank and empty it completely.
Return the fewest seconds after which the total amount of water in all tanks can be at most limit, or -1 if that can never happen. If the total is already at most limit, the answer is 0.levels=[4, 1, 2], rates=[1, 3, 0], limit=5 → 2
Second 1: levels [5, 4, 2], empty tank 1 to get [5, 0, 2], total 7. Second 2: [6, 3, 2], empty tank 0 to get [0, 3, 2], total 5. In one second the best possible total is 6.
levels=[2, 2], rates=[5, 5], limit=3 → -1
After every second the tank not emptied in that second holds at least 5, so the total never gets down to 3.
levels=[3, 4], rates=[1, 1], limit=10 → 0
The total of 7 is already within 10, so no time is needed.
levels=[4, 1, 2], rates=[1, 3, 0], limit=5
2
Run checks the first 3 tests in your browser. Submit checks all 114 tests on the server, hidden ones included.