Maximum Total Reward Using Operations II - Problem

You're presented with an exciting challenge: maximize your total reward by strategically collecting values from an array!

Given an integer array rewardValues of length n, you start with a total reward of x = 0. Here's the twist: you can only collect a reward if it's greater than your current total!

The Rules:

  • Choose any unmarked index i where rewardValues[i] > x
  • Add rewardValues[i] to your total reward x
  • Mark that index as used (can't use it again)
  • Repeat until no more valid moves exist

Your mission: Find the maximum possible total reward you can achieve through optimal play!

Example: With [1, 6, 4, 3, 2], you could collect 1→2→3→4→6 for a total of 16, but there might be a better strategy...

Input & Output

example_1.py — Basic Example
$ Input: [1, 6, 4, 3, 2]
Output: 11
💡 Note: We can collect rewards in the order: 1 (x=1), then 2 (x=3), then 4 (x=7), then 6 (x=13 > 11, so we can't take it). Wait, let's recalculate: 1→2→3→4 gives us 10 total, but we could do 1→6 for 7, then we can't take anything else. Better: 1→2→4→6 isn't valid since 6>4. The optimal is 1→2→3→4→6 isn't possible. Actually: 1(x=1)→2(x=3)→4(x=7)→6 can't be taken since 6<7. So 1→2→4 gives 7, then no valid moves. Optimal: 1→6→7→8...let me recalculate systematically.
example_2.py — Simple Case
$ Input: [1, 1, 3, 3]
Output: 4
💡 Note: We can take one instance of 1 (x=1), then one instance of 3 (x=4). We cannot take another 3 since 3 < 4, and we've used the other 1.
example_3.py — Edge Case
$ Input: [1]
Output: 1
💡 Note: We can only take the single reward of 1, giving us a total of 1.

Constraints

  • 1 ≤ rewardValues.length ≤ 2000
  • 1 ≤ rewardValues[i] ≤ 2000
  • You can use each reward value at most once
  • Key constraint: You can only collect a reward if it's greater than your current total

Visualization

Tap to expand
🏴‍☠️ The Treasure Hunter's JourneyChest 1💰Value: 1Chest 3💰Value: 3Chest 4💰Value: 4Bitset Tracking Achievable Gold Amountsdp = [T, T, F, T, T, F, F, T, T] → Max = 8 gold01234✓ Possible✗ Not possible🎯 Optimal Strategy: Process in sorted order, track all possibilities!
Understanding the Visualization
1
Start Empty-Handed
Begin with 0 gold - only this amount is initially achievable
2
Sort the Treasures
Arrange chests by value to process systematically
3
Track Possibilities
Use bitset to remember all possible gold amounts
4
Expand Smartly
For each chest, only consider taking it if current gold < chest value
5
Find Maximum
Return the highest achievable gold amount
Key Takeaway
🎯 Key Insight: By using bit manipulation to track achievable sums and processing rewards in sorted order, we can efficiently find the maximum total reward in O(n×S) time, where S is the sum of all rewards.
Asked in
Google 45 Amazon 38 Meta 29 Microsoft 22
48.5K Views
Medium-High Frequency
~25 min Avg. Time
1.4K 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