TheRealSamuel/LeetCodeProblem
0564
1{2 "id": 2767,3 "name": "maximum_sum_with_exactly_k_elements",4 "difficulty": "Easy",5 "link": "https://leetcode.com/problems/maximum-sum-with-exactly-k-elements/",6 "date": "2023-04-15 00:00:00",7 "task_description": "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:** ``` **Input:** nums = [1,2,3,4,5], k = 3 **Output:** 18 **Explanation:** We need to choose exactly 3 elements from nums to maximize the sum. For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6] For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7] For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8] So, we will return 18. It can be proven, that 18 is the maximum answer that we can achieve. ``` **Example 2:** ``` **Input:** nums = [5,5,5], k = 2 **Output:** 11 **Explanation:** We need to choose exactly 2 elements from nums to maximize the sum. For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6] For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7] So, we will return 11. It can be proven, that 11 is the maximum answer that we can achieve. ``` **Constraints:** `1 <= nums.length <= 100` `1 <= nums[i] <= 100` `1 <= k <= 100`",8 "public_test_cases": [9 {10 "label": "Example 1",11 "input": "nums = [1,2,3,4,5], k = 3",12 "output": "18 "13 },14 {15 "label": "Example 2",16 "input": "nums = [5,5,5], k = 2",17 "output": "11 "18 }19 ],20 "private_test_cases": [21 {22 "input": [23 [24 49,25 90,26 3,27 56,28 32,29 100,30 38,31 12,32 5,33 5,34 90,35 14,36 20,37 83,38 97,39 40,40 69,41 52,42 50,43 24,44 82,45 83,46 2,47 35,48 20,49 45,50 36,51 69,52 22,53 14,54 35,55 76,56 5057 ],58 6559 ],60 "output": 858061 },62 {63 "input": [64 [65 44,66 73,67 34,68 84,69 91,70 13,71 23,72 46,73 6,74 14,75 30,76 85,77 75,78 70,79 79,80 7,81 60,82 30,83 57,84 27,85 59,86 4987 ],88 2289 ],90 "output": 223391 },92 {93 "input": [94 [95 81,96 14,97 40,98 34,99 54,100 23,101 63,102 65,103 84,104 84,105 4,106 37,107 88,108 41,109 8,110 33,111 54,112 18,113 49,114 67,115 85,116 37,117 78,118 46,119 38,120 80,121 13,122 77,123 93,124 5,125 3,126 12,127 89,128 36,129 44,130 63,131 41,132 22,133 95,134 86,135 11,136 44,137 74,138 82,139 48,140 57,141 90,142 35,143 44,144 46,145 71,146 66,147 6,148 3,149 1,150 26,151 38,152 47,153 70,154 24,155 3,156 54,157 67,158 48,159 89,160 4,161 57,162 41,163 69,164 77,165 24,166 74,167 26,168 78,169 67,170 22,171 84172 ],173 71174 ],175 "output": 9230176 },177 {178 "input": [179 [180 12,181 50,182 78,183 77,184 47,185 31,186 50,187 24,188 58,189 8,190 53,191 44,192 27,193 74,194 1,195 64,196 44,197 64,198 87,199 93,200 1,201 71,202 56,203 29,204 35,205 99,206 95,207 57,208 86,209 90,210 78,211 95,212 29,213 1,214 26,215 5,216 51,217 43,218 84,219 58,220 29,221 66,222 74,223 36,224 6,225 69,226 67,227 63,228 2,229 77,230 47,231 12,232 22,233 13,234 13,235 100,236 66,237 92,238 32,239 76,240 66,241 71,242 40,243 64,244 63,245 76,246 7,247 2,248 100,249 5,250 53,251 14,252 21,253 44,254 37,255 21,256 97,257 38,258 31,259 22260 ],261 58262 ],263 "output": 7453264 },265 {266 "input": [267 [268 3,269 69,270 95,271 56,272 57,273 13,274 12,275 63,276 17,277 22,278 96,279 67,280 5,281 32,282 79,283 5,284 43,285 39,286 26,287 53,288 32,289 82,290 95,291 1,292 68,293 94,294 21,295 93,296 54,297 75,298 36,299 19,300 43,301 93,302 5,303 16,304 28,305 84,306 17,307 44,308 11,309 96,310 73,311 37,312 54,313 92,314 79,315 56,316 58,317 33,318 82,319 67,320 44,321 82,322 41,323 83,324 55,325 42,326 16,327 46328 ],329 75330 ],331 "output": 9975332 },333 {334 "input": [335 [336 47,337 16,338 81,339 75,340 82,341 87,342 11,343 52,344 71,345 8,346 61,347 31,348 52,349 38,350 55,351 74,352 89,353 15,354 9,355 49,356 48,357 87,358 36,359 84,360 45,361 77,362 92,363 24,364 37,365 64,366 40,367 29,368 18,369 87,370 95,371 80,372 41,373 84,374 12,375 76,376 75,377 90,378 13,379 65,380 84381 ],382 15383 ],384 "output": 1530385 },386 {387 "input": [388 [389 98,390 56,391 51,392 95,393 91,394 66,395 51,396 40,397 34,398 51,399 7,400 48,401 37,402 61403 ],404 5405 ],406 "output": 500407 },408 {409 "input": [410 [411 70,412 63,413 30,414 79,415 46,416 21,417 18,418 4,419 3,420 90,421 10,422 4,423 66,424 49,425 80,426 59,427 44,428 75,429 22,430 80,431 14,432 26,433 52,434 13,435 1,436 45,437 94,438 34,439 18,440 64,441 64,442 99,443 46,444 77,445 4,446 16,447 98,448 26,449 21,450 28,451 94,452 97,453 96,454 64,455 55,456 17,457 43,458 50,459 88,460 36,461 92,462 81,463 9,464 46,465 64,466 98,467 52,468 52,469 90,470 71,471 83,472 14,473 55,474 29,475 58,476 79,477 24,478 98,479 9,480 3,481 21,482 61,483 46,484 83,485 52,486 79,487 65,488 44,489 94,490 81,491 17,492 52,493 72,494 50,495 35,496 20,497 36,498 95,499 84500 ],501 75502 ],503 "output": 10200504 },505 {506 "input": [507 [508 93,509 89,510 46,511 52,512 63,513 18,514 87,515 33,516 74,517 49,518 27,519 50,520 69,521 41,522 78,523 5,524 17,525 74,526 58,527 37,528 77,529 25,530 56,531 7,532 83,533 43,534 54,535 65,536 96,537 51,538 52,539 29,540 14,541 85,542 15,543 66,544 20,545 71,546 82,547 31,548 24,549 33,550 49,551 95,552 14,553 6,554 18,555 63,556 28,557 91,558 87,559 62,560 56,561 57,562 81,563 72,564 2,565 21,566 70,567 41,568 37,569 45,570 10,571 81,572 74,573 10,574 83,575 16,576 93,577 53,578 88,579 32,580 100,581 20,582 63,583 100,584 20,585 21,586 36,587 27,588 34,589 14,590 62,591 9,592 44,593 82,594 64,595 54,596 69,597 49,598 10,599 89,600 82,601 15,602 99,603 18,604 46,605 51,606 71607 ],608 99609 ],610 "output": 14751611 },612 {613 "input": [614 [615 9,616 97,617 21,618 77,619 53,620 18,621 74,622 32,623 9,624 82,625 52,626 49,627 18,628 9,629 28,630 7,631 62,632 42,633 94,634 29,635 6,636 81,637 4,638 82,639 55,640 33,641 100,642 34,643 30,644 50,645 13,646 80,647 48,648 68,649 86,650 6,651 61,652 7,653 92,654 75,655 63,656 17,657 8,658 77,659 97,660 36,661 88,662 83,663 99,664 88,665 90,666 27,667 16,668 19,669 92,670 35,671 31,672 62,673 75,674 32,675 96,676 6,677 68,678 47,679 82,680 90,681 88,682 53,683 66,684 6,685 50,686 9,687 82,688 1,689 25,690 67,691 31,692 31,693 11,694 82,695 43,696 90,697 15,698 19,699 97,700 43,701 46,702 93,703 85,704 62,705 97,706 21707 ],708 8709 ],710 "output": 828711 }712 ],713 "haskell_template": "maximizeSum :: [Int] -> Int -> Int\nmaximizeSum nums k ",714 "ocaml_template": "let maximizeSum (nums: int list) (k: int) : int = ",715 "scala_template": "def maximizeSum(nums: List[Int],k: Int): Int = { \n \n}",716 "java_template": "class Solution {\n public int maximizeSum(int[] nums, int k) {\n \n }\n}",717 "python_template": "class Solution(object):\n def maximizeSum(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "718}