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
mfromnums. - Remove the selected element
mfrom the array. - Add a new element with a value of
m + 1to the array. - Increase your score by
m.
Return the maximum score you can achieve after performing the operation exactly k times.
Example 1
Input
nums = [1,2,3,4,5], k = 3Output
18Choosing 5, then 6, then 7 gives a maximum score of 5 + 6 + 7 = 18.
Example 2
Input
nums = [5,5,5], k = 2Output
11Choosing 5, then 6 gives a maximum score of 5 + 6 = 11.
Constraints
- 1 <= nums.length <= 100
- 1 <= nums[i] <= 100
- 1 <= k <= 100