Minimum Array Sum

You are given an integer array nums and three integers k, op1, and op2.

You can perform the following operations on nums:

  • Operation 1: Choose an index i and divide nums[i] by 2, rounding up to the nearest whole number. You can perform this operation at most op1 times, and not more than once per index.
  • Operation 2: Choose an index i and subtract k from nums[i], but only if nums[i] is greater than or equal to k. You can perform this operation at most op2 times, and not more than once per index.

Note: Both operations can be applied to the same index, but at most once each.

Return the minimum possible sum of all elements in nums after performing any number of operations.

Example 1
Inputnums = [2,8,3,19,3], k = 3, op1 = 1, op2 = 1
Output23
Applying Operation 2 to nums[1] = 8 and Operation 1 to nums[3] = 19 gives [2, 5, 3, 10, 3], whose sum is 23.
Example 2
Inputnums = [2,4,3], k = 3, op1 = 2, op2 = 1
Output3
Applying Operation 1 to nums[0] and nums[1], and Operation 2 to nums[2], gives [1, 2, 0], whose sum is 3.

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 10^5
  • 0 <= k <= 10^5
  • 0 <= op1, op2 <= nums.length

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