My Solution
\n/**\n * let `n` be the length of the vector `nums1`,\n * which is the length of the vector `nums2` as well.\n *\n * some observations\n * 1. if `accumulate(nu
```\n/**\n * let `n` be the length of the vector `nums1`,\n * which is the length of the vector `nums2` as well.\n *\n * some observations\n * 1. if `accumulate(nums1.begin(), nums1.end(), 0)` is less than or equal to `x`,\n *    no operations are needed, so `0` should be returned.\n * 2. for any specific index `i`, 0 <= `i` <= `n` - 1,\n *    at most one operation is needed to be performed on it,\n *    so the maximum return value should be `n`\n * 3. assume that at some specific seconds `s`,\n *    the sum `sum` of all elements of `nums1` is less than or equal to `x`,\n *    and the indices on each of which one operation is performed are\n *    `i1`, `i2`, ..., `is`\n *    where `sum` = (nums1[0] + s * nums2[0]) +\n *                  (nums1[1] + s * nums2[1]) +\n *                  ... +\n *                  (nums1[n - 1] + s * nums2[n - 1]) -\n *                  operations\n *          where `operations` is the value subtracted by\n *                performing `s` operations on the indices `i1`, `i2`, ..., `is`\n *    assume the following operations sequence is optimal,\n *    1) on the first second, the operation is performed on the index `i1`,\n *       so the value `nums1[i1] + 1 * nums2[i1]` should be subtracted\n *    2) on the second second, the operation is performed on the index `i2`,\n *       so the value `nums1[i2] + 2 * nums2[i2]` should be subtracted\n *    ...\n *    s) on the `s`\'th second, the operation is performed on the index `is`,\n *       so the value `nums1[is] + s * nums2[is]` should be subtracted\n *    so `operations` = (nums1[i1] + 1 * nums2[i1]) +\n *                      (nums1[i2] + 2 * nums2[i2]) +\n *                      ... +\n *                      (nums1[is] + s * nums2[is])\n *                    = (nums1[i1] + nums1[i2] + ... + nums1[is]) +\n *                      (1 * nums2[i1] + 2 * nums2[i2] + ... + s * nums2[is])\n *    in order to make `operations` maximal, nums2[is] > nums2[is - 1] > ... > nums2[i1]\n *\n * firstly sort `nums2`, and adjust `nums1` accordingly\n * then the dp solution is employed\n * dp[seconds][length] stands for the maximal value of `operations`\n * when time is `seconds` and `seconds` operations are performed on the sub-vector\n * from the index `0` to the index `length - 1`\n * where `seconds` is in the range [0, `n`], both inclusive\n *       `length` is in the range [`seconds`, `n`], both inclusive\n *\n * initial:\n * dp[0][length] = 0, where `length` is in the range [0, `n`], both inclusive\n *\n * induction:\n * dp[seconds][length] = max(dp[seconds][length - 1],\n *                           dp[seconds - 1][length - 1] + nums1[length - 1] + seconds * nums2[length - 1])\n * some explanations\n * there are two options, `option1` and `option2`, for the index `index`, which is equal to `length - 1`\n * 1. no operation is performed on it\n *    option1 = dp[seconds][length - 1]\n * 2. one operation is performed on it\n *    option2 = dp[seconds - 1][length - 1] + nums1[index] + seconds * nums2[index]\n * dp[seconds][length] should be the minimum value between `option1` and `option2`\n *\n * target:\n * for iterate every `seconds` from `0` to `n`\n * 1. if `total` - `dp[seconds][n]` is less than or equal to `x`, the `seconds` should be the return value\n * 2. otherwise, when the loop finishes, `-1` should be the return value\n * where `total` is (`accumulate(nums1.begin(), nums1.end(), 0)` + `seconds` * accumulate(nums2.begin(), nums2.end(), 0))\n *\n * Time Complexity: O(n * n)\n * Space Complexity: O(n)\n */\nclass Solution {\n public:\n  int minimumTime(const vector<int> &nums1, const vector<int> &nums2, const int x) {\n    constexpr int range = 2;\n    int total = accumulate(nums1.begin(), nums1.end(), 0);\n    if (total <= x) {\n      return 0;\n    }\n    \n    const int total2 = accumulate(nums2.begin(), nums2.end(), 0);\n    const int n = static_cast<int>(nums1.size());\n    int indices[n];\n    iota(indices, indices + n, 0);\n    sort(indices, indices + n, [&nums2](const int lhs, const int rhs) -> bool {\n      return nums2[lhs] < nums2[rhs];\n    });\n    int dp[range][n + 1];\n    memset(dp, 0, sizeof(dp));\n    int previous = 0;\n    int current = 1;\n    for (int seconds = 1; seconds < n + 1; ++seconds) {\n      for (int length = seconds; length < n + 1; ++length) {\n        const int index = indices[length - 1];\n        dp[current][length] = max(dp[current][length - 1],\n                                  dp[previous][length - 1] + nums1[index] + seconds * nums2[index]);\n      }\n      \n      total += total2;\n      if (total - dp[current][n] <= x) {\n        return seconds;\n      }\n      \n      previous ^= 1;\n      current ^= 1;\n      memset(dp[current], 0, sizeof(dp[current]));\n    }\n    return -1;\n  }\n};\n```
[]