Python solution (DP tab,  explained)
Approach\n Describe your approach to solving the problem. \nLet\'s consider the following example:\n- nums1 = [100, 5, 1, 1]\n- nums2 = [2, 1, 3, 5]\n\nSome obs
# Approach\n<!-- Describe your approach to solving the problem. -->\nLet\'s consider the following example:\n- `nums1 = [100, 5, 1, 1]`\n- `nums2 = [2, 1, 3, 5]`\n\nSome observations:\n1. At time `0` the result is `sum(nums1)` regardless of what the `nums2` elements are. If `sum(nums1) <= x`, the problem condition is satisfied at time `0`, so we just return `0`\n\n2. At time `t` the result can be represented by two parts:\n  + the positive part which includes initial `sum(nums1)` plus increment, which is the same each second and equal to `sum(nums2)`. I.e., at time `t` the positive part is equal to `sum(nums1) + t * sum(nums2)`\n  + the negative part due to performing the "reset" operation: setting the current value of element `j` (which at time `t` is equal to `nums1[j] + t * nums2[j]`) to `0`\n\n3. After applying the operation at selected index `j`, further value of this element does not depend on initial `nums1[j]` value and can be calculated as `nums2[j]` multiplied by time since the reset operation\n  + this suggests that if we did not succeed after applying operation to each index (i.e., after `len(nums1)` seconds), the sum of array elements can not be mimimized to `<= x`\n\n4. From previous observation it is clear that if we need to reset several indices `j1 < j2 < j3 ...` it is reasonable to select them such that `nums2[j1] <= nums2[j2] <= nums2[j3] ...`. In this case the newly accumulated sum of `nums2[j] * time since reseting element at index j` for these indices will be minimal, that minimizes the overall result\n\n5. Item 4 suggests we have to sort `nums2` in non-decreasing order and rearrange `nums1` accordingly\n\n\nAfter sorting `nums2` and rearranging `nums1` we will get:\n- `nums1 = [5, 100, 1, 1]`\n- `nums2 = [1, 2, 3, 5]`\n\nIf we are given only 1 second, we will reduce the result by:\n- `5 + 1*1` if index `j = 0` is selected to perform operation;\n- `100 + 2*1` if index `j = 1` is selected, and so on.\n\nObviously, moving from smaller indeces to the larger ones we select the maximum value that can be reset from the value on the left and the one that can be obtained for current index. It looks like we can find our negative part of solution using dynamic programming.\n\nFor initial row we need a list of `0` of `len(nums1) + 1` size. I.e., for time `t = 0` and `t = 1` from the sample above we will get<br>\n`t=0: [0, 0, 0, 0, 0]`\n`t=1: [0, 6, 102, 102, 102]`\n\nThe result will be `sum(nums1) + sum(nums2) * t - dp[t=1][-1]` or `107 + 11 * 1 - 102 = 16`\n\nFor `t=2` since we agreed that it is reasonable to select indeces to reset in non-decreasing order of `nums2` we can write:<br>\n`dp[t][j+1] = max( dp[t][j], dp[t-1][j] + nums1[j] + t * nums2[j]) )` for `j in range(len(nums1))`\n\nThus we will get:<br>\n`t=2: [0, 7, 110, 110, 113]`, `result = 107 + 11 * 2 - 113 = 16`<br>\n`t=3: [0, 8, 113, 120, 126]`, `result = 107 + 11 * 3 - 126 = 14`<br>\n`t=4: [0, 9, 116, 126, 141]`, `result = 107 + 11 * 4 - 141 = 10`\n\nLet\'s check what happens if we continue:<br>\n`t=5: [0,10, 119, 132, 152]`, `result = 107 + 11 * 5 - 152 = 10`<br>\n`t=6: [0,11, 122, 138, 163]`, `result = 107 + 11 * 6 - 163 = 10`\n\nThat\'s exactly what was expected, we can not minimize the result any further\n# Complexity\n- Time complexity: O(n<sup>2</sup>), `n = len(nums1)`\n\n- Space complexity: O(n<sup>2</sup>) if we use dp matrix or O(n) if we use previous and current `dp` rows as in solution below\n\n# Code\n```python\nclass Solution:\n    def minimumTime(self, nums1: List[int], nums2: List[int], x: int) -> int:\n        # calculate sums for nums1, nums2\n        sum1 = sum(nums1)\n        if sum1 <= x:\n            return 0\n        sum2 = sum(nums2)\n        n = len(nums1)\n        \n        # sort nums2 and rearrange nums1 accordingly\n        nums2, nums1 = zip(*sorted(zip(nums2, nums1)))    #type: ignore\n\n        dp1 = [0]*(n+1)\n        dp2 = [0]*(n+1)\n        \n        # use DP tabulation to calculate max value that can be reduced for first t seconds\n        for t in range(1, n+1):\n            for j in range(n):\n                dp2[j+1] = max(dp2[j], dp1[j] + nums1[j] + nums2[j] * t)\n            sum1 += sum2\n            if sum1 - dp2[-1] <= x:\n                return t\n            dp1, dp2 = dp2, dp1\n        return -1\n    #end minimumTime(...)\n```
['Python3']