Maximum Sum With Exactly K Elements

You are given a 0-indexed integer array nums and an integer k. Your task is to perform the following operation exactly k times in order to maximize your score:

  • Select an element m from nums.
  • Remove the selected element m from the array.
  • Add a new element with a value of m + 1 to the array.
  • Increase your score by m.

Return the maximum score you can achieve after performing the operation exactly k times.

Example 1
Inputnums = [1,2,3,4,5], k = 3
Output18
Choosing 5, then 6, then 7 gives a maximum score of 5 + 6 + 7 = 18.
Example 2
Inputnums = [5,5,5], k = 2
Output11
Choosing 5, then 6 gives a maximum score of 5 + 6 = 11.

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100
  • 1 <= k <= 100

Asked at 1 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