Maximum Sum With Exactly K Elements - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [5,2,1], k = 3
Output: 18
💡 Note: Operation 1: Select 5 (max), score = 5, array becomes [6,2,1]. Operation 2: Select 6 (max), score = 11, array becomes [7,2,1]. Operation 3: Select 7 (max), score = 18. Total score = 5 + 6 + 7 = 18.
Example 2 — Single Element
$ Input: nums = [10], k = 2
Output: 21
💡 Note: Operation 1: Select 10, score = 10, array becomes [11]. Operation 2: Select 11, score = 21. Total score = 10 + 11 = 21.
Example 3 — Larger Array
$ Input: nums = [1,2,3,4,5], k = 4
Output: 26
💡 Note: Always select maximum: 5 + 6 + 7 + 8 = 26. The other elements don't matter since we keep incrementing the maximum.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 100
  • 1 ≤ k ≤ 100

Visualization

Tap to expand
Maximum Sum Problem Overview521Input: nums = [5,2,1], k = 3Select Maximum 3 Times: 5 → 6 → 7567Output: 5 + 6 + 7 = 18
Understanding the Visualization
1
Input
Array [5,2,1] with k=3 operations
2
Process
Always select maximum, then increment it
3
Output
Sum of selected elements: 18
Key Takeaway
🎯 Key Insight: Always selecting the maximum element and incrementing it creates an arithmetic sequence that maximizes the total score
Asked in
Google 25 Amazon 18
23.1K Views
Medium Frequency
~10 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen