2656. Maximum Sum With Exactly K Elements Easy
1/**
2 * [2656] Maximum Sum With Exactly K Elements
3 *
4 * 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:
5 * <ol>
6 * Select an element m from nums.
7 * Remove the selected element m from the array.
8 * Add a new element with a value of m + 1 to the array.
9 * Increase your score by m.
10 * </ol>
11 * Return the maximum score you can achieve after performing the operation exactly k times.
12 *
13 * <strong class="example">Example 1:
14 *
15 * Input: nums = [1,2,3,4,5], k = 3
16 * Output: 18
17 * Explanation: We need to choose exactly 3 elements from nums to maximize the sum.
18 * For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]
19 * For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]
20 * For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]
21 * So, we will return 18.
22 * It can be proven, that 18 is the maximum answer that we can achieve.
23 *
24 * <strong class="example">Example 2:
25 *
26 * Input: nums = [5,5,5], k = 2
27 * Output: 11
28 * Explanation: We need to choose exactly 2 elements from nums to maximize the sum.
29 * For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]
30 * For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]
31 * So, we will return 11.
32 * It can be proven, that 11 is the maximum answer that we can achieve.
33 *
34 *
35 * Constraints:
36 *
37 * 1 <= nums.length <= 100
38 * 1 <= nums[i] <= 100
39 * 1 <= k <= 100
40 *
41 *
42 * <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
43 * }
44 * .spoiler {overflow:hidden;}
45 * .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
46 * .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
47 * .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
48 * </style>
49 *
50 */
51pub struct Solution {}
52
53// problem: https://leetcode.com/problems/maximum-sum-with-exactly-k-elements/
54// discuss: https://leetcode.com/problems/maximum-sum-with-exactly-k-elements/discuss/?currentPage=1&orderBy=most_votes&query=
55
56// submission codes start here
57
58impl Solution {
59 pub fn maximize_sum(nums: Vec<i32>, k: i32) -> i32 {
60 0
61 }
62}
63
64// submission codes end
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_2656() {
72 }
73}
74
Back
© 2025 bowen.ge All Rights Reserved.