#2530

Maximal Score After Applying K Operations

specialist · 650 · lc medium +30 · verified · 64% accepted · 878 likes · top 67%

Description

Given a 0-indexed integer array nums and integer k, start with a score of 0. In each of exactly k operations, pick any index i, add nums[i] to your score, then replace nums[i] with ceil(nums[i] / 3). Return the maximum score achievable.

Example 1:

Input: nums = [10,10,10,10,10], k = 5
Output: 50
Explanation: Apply the operation to each array element exactly once. The final score is 10 + 10 + 10 + 10 + 10 = 50.

Example 2:

Input: nums = [1,10,3,3,3], k = 3
Output: 17
Explanation: You can do the following operations:
Operation 1: Select i = 1, so nums becomes [1,4,3,3,3]. Your score increases by 10.
Operation 2: Select i = 1, so nums becomes [1,2,3,3,3]. Your score increases by 4.
Operation 3: Select i = 2, so nums becomes [1,2,1,3,3]. Your score increases by 3.
The final score is 10 + 4 + 3 = 17.

Code

1
2
3