LeetCode solutions

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

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 2ms, memory 17.2MB, accepted 2025-12-31.

python
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

Source