C++ solution with explainations
Intuition\nSome key observations: \n\nKey 1: The number of operations required should be no more than the length of the array. \n\nKey 2: There exist an optimiz
# Intuition\nSome key observations: \n\n**Key 1**: The number of operations required **should be no more than the length of the array**. \n\n**Key 2**: There exist an optimize way that **apply the operation to each indexes in the array atmost 1 time**. \n\n**Key 3**: Let\'s say for t operations, we are choosing to apply for t indexes: i1, i2, ..., it, the **one with higher nums2[] value will be apply later**. \n\nIndeed, let\'s say if nums2[i2] > nums2[i1] and operation applied for i1 at time t1, applied for i2 at time t2 where t1 > t2. Then if we applied the operation for i1 at t2, and for i2 at t1, we will have better result. (can write the difference between them to compare)\n\n\n# Approach\n\nWe will first sort the **pairs (nums2[i], nums1[i])** in increasing order of nums2[i]. \n\nWe will define a dp[time][n] as the maximum value we could deduced from the total sum (which is sigma(nums1[i]) + time * sigma(nums2[i])) if we apply the operations for **time** times consider the index up to n. \n\nThen the transition is: \n\n```\ndp[i][t] = max(dp[i-1][t], V[i-1].first * t + V[i-1].second + dp[i-1][t-1]);\n```\n\nif we don\'t includes the index i to the operations, \n```\n    dp[i][t] = dp[i-1][t]\n``` \nif we includes the index i to the operations, as base on the key 1, the one with higher nums2[i] should have done later - which in this case, as times == t -> the remove is V[i-1].first **(this one is nums2[] value)** * t +  V[i-1].second **(this one is nums1[] value)**\n```\n    dp[i][t] = V[i-1].first * t + V[i-1].second + dp[i-1][t-1]\n``` \n\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(n^2)\n\n# Code\n```\nclass Solution {\npublic:\n    int minimumTime(vector<int>& nums1, vector<int>& nums2, int x) {\n        int n = nums1.size();\n        int ss = accumulate(nums1.begin(),nums1.end(),0);\n        int ss2 = accumulate(nums2.begin(),nums2.end(),0);\n        if (ss <= x){\n            return 0;\n        }\n        \n        vector<pair<int, int>> V(n);\n        for (int i = 0; i<n ;i++){\n            V[i] = make_pair(nums2[i], nums1[i]);\n        }\n        \n        sort(V.begin(), V.end());\n        \n        const int oo = 2e9;\n        vector<vector<int>> dp(n + 1, vector<int>(n + 1));\n        \n        for (int t = 1; t <= n; t++){\n            for (int i = 1; i <= n; i++){\n                if (i < t){\n                    continue; \n                }\n                dp[i][t] = max(dp[i-1][t], V[i-1].first * t + V[i-1].second + dp[i-1][t-1]);\n            }\n        }\n        \n        //int ans = oo;\n        for (int t = 1; t <= n; t++){\n            int tot = ss2 * t + ss;\n            //cout << tot << " " << dp[n][t] << "\\n";\n            if (tot - dp[n][t] <= x){\n                return t; \n            }\n        }\n        return -1;\n    }\n};\n```
['C++']