Maximum Total Sum of K Selected Elements Problem
Description
LeetCode Problem 3974.
You are given an integer array nums and two integers k and mul.
Select exactly k elements from nums. Process these elements one by one in any order you choose.
For each selected element, independently choose one of the following:
- Add the element's value to the total sum, or
- Multiply the element by the current value of
muland add the result to the total sum.
After processing each selected element, mul decreases by 1, regardless of which option was chosen. The current value of mul may become 0 or negative.
Return an integer denoting the maximum possible total sum.
Example 1:
Input: nums = [6,1,2,9], k = 3, mul = 2
Output: 26
Explanation:
One optimal way:
- One optimal selection is nums[3] = 9, nums[0] = 6, and nums[2] = 2.
- Process nums[3] = 9 first: choose multiplication, so it contributes 9 * 2 = 18. Now, mul becomes 1.
- Process nums[0] = 6 next: choose multiplication, so it contributes 6 * 1 = 6. Now, mul becomes 0.
- Process nums[2] = 2 last: choose addition, so it contributes 2.
- The total sum is 18 + 6 + 2 = 26.
Example 2:
Input: nums = [3,7,5,2], k = 2, mul = 4
Output: 43
Explanation:
One optimal way:
- One optimal selection is nums[1] = 7 and nums[2] = 5.
- Process nums[1] = 7 first: choose multiplication, so it contributes 7 * 4 = 28. Now, mul becomes 3.
- Process nums[2] = 5 next: choose multiplication, so it contributes 5 * 3 = 15.
- The total sum is 28 + 15 = 43.
Example 3:
Input: nums = [4,4], k = 1, mul = 1
Output: 4
Explanation:
One optimal way:
- One optimal selection is nums[0] = 4.
- Process nums[0] = 4: choose multiplication, so it contributes 4 * 1 = 4.
- The total sum is 4.
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^51 <= k <= nums.length1 <= mul <= 10^5
Solution
Approach: Greedy + Sorting
Each chosen element is worth nums[i] * mul when multiplied and just nums[i] when added, so it gets the better of the two, which is nums[i] * max(1, mul). Once mul drops to 1 or below, every remaining element is simply added.
The multiplier gets smaller with every step, so we want the largest elements to meet the largest multipliers. Sort the array and take the k largest elements, processing them from the biggest down: the first one uses mul, the next one mul - 1, and so on. Swapping a larger element with a smaller one that comes earlier can never help, so this order is optimal, and choosing the k largest values is optimal as well.
The total can reach about 1015, so use a 64-bit integer in Java, C++ and Go.
Complexity: O(n log n) time for sorting and O(1) extra space besides the sort, where n is the length of nums.
class Solution:
def maxSum(self, nums: List[int], k: int, mul: int) -> int:
nums.sort()
n = len(nums)
ans = 0
for i in range(n - 1, n - 1 - k, -1):
ans += nums[i] * max(1, mul)
mul -= 1
return ans