LeetCode solutions

2616. Maximal Score After Applying K Operations

My accepted Python solution to LeetCode problem 2616, Maximal Score After Applying K Operations, running in 256ms.

  • Difficulty: Medium
  • Python
  • Runtime 256ms
  • Memory 33.7MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 256ms, memory 33.7MB, accepted 2025-12-30.

python
class Solution:
    def maxKelements(self, nums: List[int], k: int) -> int:
        import heapq
        import math
        
        # Use negative values for max heap
        heap = [-x for x in nums]
        heapq.heapify(heap)
        
        score = 0
        for _ in range(k):
            # Get the largest element
            largest = -heapq.heappop(heap)
            score += largest
            # Replace with ceil(largest / 3)
            new_val = math.ceil(largest / 3)
            heapq.heappush(heap, -new_val)
        
        return score

Source