[LeetCode] Maximum Total Sum of K Selected Elements

3974. Maximum Total Sum of K Selected Elements

You are given an integer array nums and two integers k and mul.

Select exactly k elements from nums. Process these elements one by one in any order you choose.

For each selected element, independently choose one of the following:

  • Add the element’s value to the total sum, or
  • Multiply the element by the current value of mul and add the result to the total sum.

After processing each selected element, mul decreases by 1, regardless of which option was chosen. The current value of mul may become 0 or negative.

Return an integer denoting the maximum possible total sum.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
long long maxSum(vector<int>& nums, int k, long long mul) {
sort(rbegin(nums), rend(nums));
long long res = 0;
for(int i = 0; i < k; i++) {
res += nums[i] * max(1ll, mul);
mul--;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/maximum-total-sum-of-k-selected-elements/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.