You are given an integer array nums and an integer k. Your score starts at 0.
In one operation you choose any index i with 0 <= i < len(nums), add nums[i] to your score, and replace nums[i] with ceil(nums[i] / 3), the smallest integer greater than or equal to nums[i] / 3. The same index may be chosen in more than one operation.
Return the maximum score you can reach after applying exactly k operations.
Function Signature
def max_k_elements(nums: list[int], k: int) -> int:
Constraints
-
1 <= len(nums) <= 10^5
-
1 <= k <= 10^5
-
1 <= nums[i] <= 10^9
Examples
Input: nums = [10, 10, 10, 10, 10], k = 5
Output: 50
Each of the five operations can take a different 10, so the score is 5 times 10, which is 50. Taking a 10 that has already been reduced to 4 would only lower the total.
Input: nums = [1, 10, 3, 3, 3], k = 3
Output: 17
One best sequence takes 10 (score 10, it becomes 4), then that 4 (score 14, it becomes 2), then one of the 3s (score 17). No other sequence of three operations scores more.
Input: nums = [5], k = 4
Output: 9
The only element is taken four times as 5, 2, 1 and 1, so the score is 5 + 2 + 1 + 1 = 9. A value of 1 stays 1 after an operation.