levels and a list of steps drops, where drops[j] = [l, r, val]. In a step you may pick any set of cells with indices from l to r inclusive (possibly none) and lower each picked cell by exactly val. Going below zero is not allowed: every cell has to land exactly on zero.
Steps are applied strictly in order. Find the smallest k ≥ 0 such that after the first k steps the array can be made all zeros. If that is impossible even with every step, return -1.levels=[3, 2], drops=[[0, 1, 2], [0, 0, 1], [1, 1, 5], [0, 1, 1]] → 2
The first step takes exactly 2 from both cells, giving [1, 0]. The second takes 1 from cell 0, and the array is all zeros, so the answer is 2.
levels=[4], drops=[[0, 0, 3], [0, 0, 3]] → -1
Steps of 3 can only add up to 0, 3 or 6, never exactly 4.
levels=[0, 0, 0], drops=[[0, 2, 1]] → 0
The array is already all zeros, so the answer is 0.
levels=[3, 2], drops=[[0, 1, 2], [0, 0, 1], [1, 1, 5], [0, 1, 1]]
2
Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.