<p>You are given an array of non-negative integers <code>nums</code> and an integer <code>k</code>. In one operation, you may choose <strong>any</strong> element from <code>nums</code> and <strong>increment</strong> it by <code>1</code>.</p>

<p>Return<em> the <strong>maximum</strong> <strong>product</strong> of </em><code>nums</code><em> after <strong>at most</strong> </em><code>k</code><em> operations. </em>Since the answer may be very large, return it <b>modulo</b> <code>10<sup>9</sup> + 7</code>. Note that you should maximize the product before taking the modulo.&nbsp;</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,4], k = 5
<strong>Output:</strong> 20
<strong>Explanation:</strong> Increment the first number 5 times.
Now nums = [5, 4], with a product of 5 * 4 = 20.
It can be shown that 20 is maximum product possible, so we return 20.
Note that there may be other ways to increment nums to have the maximum product.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [6,3,3,2], k = 2
<strong>Output:</strong> 216
<strong>Explanation:</strong> Increment the second number 1 time and increment the fourth number 1 time.
Now nums = [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 = 216.
It can be shown that 216 is maximum product possible, so we return 216.
Note that there may be other ways to increment nums to have the maximum product.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length, k &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
</ul>

"""

nums = [x1, x2, x3, x4]
       [1, 4, 10, 0]
k = 2
First, choose 0s to increment!!
       [1, 4, 10, 1]

2*4*10 = 80
5*10
4*11
-> Increment the lowest number at each step k

k=3
[0, 0, 2, 1]

Easier: Use a heap to store the lowest elements. -> O(n*log(k))
 Then, for k steps, pop the min, increase it and put it again in the
 heap (updated). -> O(k*log(k)) 

"""

import heapq

class Solution:
    def maximumProduct(self, nums: List[int], k: int) -> int:
        max_heap = []
        for idx, num in enumerate(nums):
            if len(max_heap) < k:
                heapq.heappush(max_heap, (-num, idx))
            elif -max_heap[0][0] > num:
                heapq.heappop(max_heap)
                heapq.heappush(max_heap, (-num, idx))
        
        min_heap = []
        while max_heap:
            num, idx = heapq.heappop(max_heap)
            heapq.heappush(min_heap, (-num, idx))
        
        print(nums)
        for _ in range(k):
            _, idx = heapq.heappop(min_heap)
            nums[idx] += 1
            heapq.heappush(min_heap, (nums[idx], idx))
        print(nums)

        product = 1
        max_module = 10**9 + 7
        for num in nums:
            product = (product % max_module) * (num % max_module)
        return product % max_module
        


1440