Maximum Total Value Problem
Description
LeetCode Problem 3971.
You are given two integer arrays value and decay, and an integer m.
value[i]represents the initial value at indexi.decay[i]represents how much the value decreases after each selection of indexi.
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 t^th 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 10^9 + 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:
- By selecting index 0, the value gained is 6.
- By selecting index 1, the value gained is 5.
- By selecting index 2, the value gained is 4.
- By selecting index 0 again, the value gained is 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:
- By selecting index 0, the value gained is 7.
- By selecting index 0 again, the value gained is 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:
- By selecting index 0, the value gained is 4.
- By selecting index 1, the value gained is 3.
The total value is 4 + 3 = 7.
Constraints:
1 <= value.length == decay.length <= 10^51 <= value[i], decay[i] <= 10^91 <= m <= 10^9
Solution
Approach: Binary Search on the Threshold
Each index i gives a decreasing stream of gains value[i], value[i] - decay[i], value[i] - 2 * decay[i], and so on. Picking an index for the t-th time always takes the next gain of that stream, and since each stream is decreasing, the best m picks are simply the m largest positive gains over all streams. With m up to 109 we cannot take them one by one with a heap, so we find a threshold instead.
Let count(t) be the number of gains that are at least t. For index i this is (value[i] - t) / decay[i] + 1 when value[i] >= t, and 0 otherwise. It never increases as t grows, so binary search for the smallest t >= 1 with count(t) <= m.
Then:
- Take every gain that is at least
t. For an index withcsuch gains, their sum isc * value[i] - decay[i] * c * (c - 1) / 2, an arithmetic series. - If
t > 1, thencount(t - 1) > mby the choice oft, so there are more than enough gains equal to exactlyt - 1. Fill the remainingm - count(t)picks with them. - If
t = 1, every positive gain is taken and fewer thanmpicks are used.
Take the result modulo 109 + 7, reducing the products as you go to avoid overflow.
Complexity: O(n log V) time and O(1) extra space, where V is the largest value.
class Solution:
def maxTotalValue(self, value: List[int], decay: List[int], m: int) -> int:
MOD = 10**9 + 7
# number of selections that gain at least t
def count(t: int) -> int:
return sum((v - t) // d + 1 for v, d in zip(value, decay) if v >= t)
# smallest t >= 1 such that at most m selections gain t or more
lo, hi = 1, max(value) + 1
while lo < hi:
mid = (lo + hi) // 2
if count(mid) <= m:
hi = mid
else:
lo = mid + 1
t = lo
total = 0
taken = 0
for v, d in zip(value, decay):
if v >= t:
c = (v - t) // d + 1
taken += c
total += c * v - d * c * (c - 1) // 2
if t > 1:
total += (m - taken) * (t - 1) # the remaining picks each gain exactly t - 1
return total % MOD