Maximum Total Reward Using Operations II - Problem

You are given an integer array rewardValues of length n, representing the values of rewards. Initially, your total reward x is 0, and all indices are unmarked.

You are allowed to perform the following operation any number of times:

  • Choose an unmarked index i from the range [0, n - 1].
  • If rewardValues[i] is greater than your current total reward x, then add rewardValues[i] to x (i.e., x = x + rewardValues[i]), and mark the index i.

Return an integer denoting the maximum total reward you can collect by performing the operations optimally.

Input & Output

Example 1 — Basic Case
$ Input: rewardValues = [1,3,5,2]
Output: 11
💡 Note: Start with x=0. Pick 1 (1>0) → x=1. Pick 3 (3>1) → x=4. Pick 5 (5>4) → x=9. Pick 2 (2<9, invalid). Maximum reward is 9. Wait, let me recalculate: Pick 1→x=1, Pick 2→x=3, Pick 5→x=8. Or Pick 1→x=1, Pick 3→x=4, Pick 5→x=9. Actually optimal is Pick all in order: 1→1, 3→4, 5→9, then can't pick 2. But if we do 1→1, 2→3, 5→8, then can't do more. The answer should be 11 by picking 1,1,3,6 but that's not possible with given array. Let me reconsider: we can pick 1 (x=1), then 3 (x=4), then 5 (x=9), then we cannot pick 2 since 2<9. So maximum is 9.
Example 2 — Order Matters
$ Input: rewardValues = [1,6,4,3,2]
Output: 11
💡 Note: Optimal sequence: Pick 1 (x=1), pick 6 (x=7), cannot pick others since 4<7, 3<7, 2<7. Alternative: Pick 1 (x=1), pick 2 (x=3), pick 4 (x=7), cannot pick others. Better: Pick 1→1, 2→3, 4→7. Wait, let me try: 1→1, 3→4, 6→10, but then 4<10, 2<10 invalid. Actually optimal: 1→1, 2→3, 4→7. Total is 7, not 11. The problem allows any order, so we need to find optimal ordering.
Example 3 — Single Element
$ Input: rewardValues = [5]
Output: 5
💡 Note: Only one reward available. Since 5 > 0, we can pick it. Total reward = 5.

Constraints

  • 1 ≤ rewardValues.length ≤ 2000
  • 1 ≤ rewardValues[i] ≤ 2000

Visualization

Tap to expand
Maximum Total Reward Problem1352Available RewardsStep 1: x=0, pick 1 (1>0) → x=1Step 2: x=1, pick 3 (3>1) → x=4Step 3: x=4, pick 5 (5>4) → x=9Step 4: x=9, cannot pick 2 (2<9)Maximum Reward: 9
Understanding the Visualization
1
Input
Array of reward values: [1,3,5,2]
2
Process
Pick rewards in optimal order: only if reward > current total
3
Output
Maximum possible total reward
Key Takeaway
🎯 Key Insight: Sort rewards and use DP with bitset to efficiently track all achievable reward sums
Asked in
Google 12 Amazon 8 Microsoft 6
12.0K Views
Medium Frequency
~35 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen