You are given two integer arrays value and decay, and an integer m.
value[i] represents the initial value at index i.decay[i] represents how much the value decreases after each selection of index i.You may select any index multiple times. The total number of selections across all indices must not exceed m.
If you select index i for the tth time, where t is 1-indexed, the value gained is value[i] - decay[i] * (t - 1).
Return the maximum total value you can obtain. Since the answer may be large, return it modulo 109 + 7.
Example 1:
Input: value = [6,5,4], decay = [2,1,1], m = 4
Output: 19
Explanation:
One optimal sequence of selections is as follows:
6 - 2 = 4.The total value is 6 + 5 + 4 + 4 = 19. No other sequence of at most 4 selections gives a higher total value.
Example 2:
Input: value = [7,2,2], decay = [3,2,1], m = 2
Output: 11
Explanation:
One optimal sequence of selections is as follows:
7 - 3 = 4.The total value is 7 + 4 = 11.
Example 3:
Input: value = [4,3], decay = [5,4], m = 5
Output: 7
Explanation:
One optimal sequence of selections is as follows:
The total value is 4 + 3 = 7.
Constraints:
1 <= value.length == decay.length <= 1051 <= value[i], decay[i] <= 1091 <= m <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks us to select at most m positive values generated by decaying arithmetic progressions from given initial values. Since we want to maximize the sum, we should greedily pick the largest available values across all options until either no positive values remain or m selections are made. We can binary search for a threshold value such that the total count of elements greater than or equal to this threshold matches our target count.
Here's how the algorithm would work step-by-step:
def maximum_total_value(value, decay, m):
modulus = 10**9 + 7
# Binary search for the cutoff value of the elements chosen
low_bound = 1
high_bound = max(value)
cutoff = 1
while low_bound <= high_bound:
mid_val = (low_bound + high_bound) // 2
count = 0
for val, dec in zip(value, decay):
if val >= mid_val:
count += (val - mid_val) // dec + 1
if count >= m:
cutoff = mid_val
low_bound = mid_val + 1
else:
high_bound = mid_val - 1
total_gain = 0
total_taken = 0
# Sum up all selections strictly greater than the cutoff value
for val, dec in zip(value, decay):
if val > cutoff:
selections = (val - (cutoff + 1)) // dec + 1
total_taken += selections
last_term = val - (selections - 1) * dec
total_gain += selections * (val + last_term) // 2
# Add the remaining selections at the cutoff threshold
remaining = min(m - total_taken, sum((val - cutoff) // dec + 1 for val, dec in zip(value, decay) if val >= cutoff) - total_taken)
if cutoff > 0 and remaining > 0:
total_gain += remaining * cutoff
return total_gain % modulus
We want to pick the top elements produced by multiple arithmetic progressions, where each selection yields non-negative gains. Because the total number of operations can be up to 10^9, we cannot simulate the process greedily with a heap. Instead, we use binary search to determine a cutoff value such that the total number of terms strictly greater than or equal to this threshold is at most m.
Here's how the algorithm would work step-by-step:
def max_total_value(value: list[int], decay: list[int], m: int) -> int:
mod = 10**9 + 7
low = 1
high = 10**9
threshold = 10**9 + 1
# Binary search to find the minimum value threshold we can include.
while low <= high:
mid = (low + high) // 2
count = 0
for val, dec in zip(value, decay):
if val >= mid:
count += (val - mid) // dec + 1
if count <= m:
threshold = mid
high = mid - 1
else:
low = mid + 1
total_gain = 0
items_taken = 0
# Greedily take all gains that are strictly above the threshold.
for val, dec in zip(value, decay):
if val >= threshold:
terms = (val - threshold) // dec + 1
items_taken += terms
first_term = val
last_term = val - (terms - 1) * dec
total_gain += (first_term + last_term) * terms // 2
# Fill the remaining available moves with the threshold value.
remaining_moves = min(m - items_taken, sum((val - (threshold - 1)) // dec + 1 for val, dec in zip(value, decay) if val >= threshold - 1) - items_taken if threshold > 1 else 0)
if threshold > 1:
total_gain += remaining_moves * (threshold - 1)
return total_gain % mod
| Case | How to Handle |
|---|---|
| Empty items array or zero capacity limit | Return zero immediately since no items can be selected or no weight can be carried. |
| Single item where its weight exceeds the capacity | The algorithm skips this item and correctly yields a maximum value of zero. |
| Items with identical values and weights | The dynamic programming table naturally considers multiple inclusions if capacity permits. |
| Very large capacity and numerous items | Space optimization using a 1D array prevents memory limit exceed errors while maintaining correct state transitions. |
| Zero weight items present in the input | The loop includes them if their value is positive, adding free value without consuming capacity. |
| Capacity is significantly smaller than all item weights | The algorithm will process all items and return a total value of zero. |
| Extremely large item values causing potential integer overflow | Using standard 64-bit integers for the DP table accumulation prevents arithmetic overflow during summation. |