Maximum Total Sum with Threshold Constraints

You are given two integer arrays nums and threshold, each of length n.

You start with:

  • step = 1
  • total = 0
  • No index is used initially.

At each step:

  1. You may choose any unused index i such that threshold[i] <= step.
  2. If no such index exists, the process stops.
  3. Add nums[i] to total.
  4. Mark index i as used.
  5. Increment step by 1.

Return the maximum total sum that can be obtained by choosing indices in an optimal order.

Example 1:

Input: nums = [8, 3, 5, 7], threshold = [1, 3, 2, 2]

Output: 23

Explanation:

  • Step 1 → Only index 0 is valid (threshold[0] = 1), Total = 8
  • Step 2 → Indices 2 and 3 are now valid. Choose index 3 (value = 7), Total = 15
  • Step 3 → Index 2 is valid. Choose index 2 (value = 5), Total = 20
  • Step 4 → Index 1 becomes valid. Choose index 1 (value = 3), Total = 23

Example 2:

Input: nums = [6, 9, 4], threshold = [2, 3, 3]

Output: 0

Explanation:

  • At Step 1, no index satisfies threshold[i] <= 1, The process stops immediately.
  • Total sum = 0.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [5, 2, 9], threshold = [1, 2, 2]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • n == nums.length == threshold.length
  • 1 ≤ n ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ threshold[i] ≤ n

Hints

Frequently Occurring Doubts

Fun Facts

0
class Solution {
public:
long long maxSum(vector<int>& nums, vector<int>& threshold) {
// Your code goes here
}
};
Test Case

Input:

Nums
Threshold