2767. Maximum Sum With Exactly K Elements
My accepted Python solution to LeetCode problem 2767, Maximum Sum With Exactly K Elements, running in 2ms.
- Difficulty: Easy
- Python
- Runtime 2ms
- Memory 17.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 2ms, memory 17.2MB, accepted 2025-12-31.
class Solution:
def maximizeSum(self, nums: List[int], k: int) -> int:
m = max(nums)
# Sum of m + (m+1) + (m+2) + ... + (m+k-1)
# = k*m + (0 + 1 + 2 + ... + k-1)
# = k*m + k*(k-1)/2
return k * m + k * (k - 1) // 2