Maximal Score After Applying K Operations

You are given a 0-indexed integer array nums and an integer k. You have a starting score of 0.

In one operation:

  • Choose an index i such that 0 <= i < nums.length.
  • Increase your score by nums[i].
  • Replace nums[i] with ceil(nums[i] / 3).

Return the maximum possible score you can attain after applying exactly k operations.

The ceiling function ceil(val) is the least integer greater than or equal to val.

Example 1
Inputnums = [10,10,10,10,10], k = 5
Output50
Apply the operation to each array element exactly once, giving a final score of 10 + 10 + 10 + 10 + 10 = 50.
Example 2
Inputnums = [1,10,3,3,3], k = 3
Output17
Select index 1 twice for scores 10 and 4, then select index 2 for score 3, for a total score of 17.

Constraints

  • 1 <= nums.length, k <= 10^5
  • 1 <= nums[i] <= 10^9

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate