<p>Given an integer array <code>nums</code> and an integer <code>k</code>, modify the array in the following way:</p>

<ul>
	<li>choose an index <code>i</code> and replace <code>nums[i]</code> with <code>-nums[i]</code>.</li>
</ul>

<p>You should apply this process exactly <code>k</code> times. You may choose the same index <code>i</code> multiple times.</p>

<p>Return <em>the largest possible sum of the array after modifying it in this way</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [4,2,3], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> Choose index 1 and nums becomes [4,-2,3].
</pre>

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

<pre>
<strong>Input:</strong> nums = [3,-1,0,2], k = 3
<strong>Output:</strong> 6
<strong>Explanation:</strong> Choose indices (1, 2, 2) and nums becomes [3,1,0,2].
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,-3,-1,5,-4], k = 2
<strong>Output:</strong> 13
<strong>Explanation:</strong> Choose indices (1, 4) and nums becomes [2,3,-1,5,4].
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>
	<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
</ul>

#2023/05/08
'''
把所有负数变成正数能最大，就要考虑负数的个数和k的关系
'''
# class Solution:
#     def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:
#         minAbs=float("inf")
#         negNum=0
#         nums.sort()
#         total=0
#         for i in range(len(nums)):
#             minAbs=min(minAbs,abs(nums[i]))
#             if nums[i]<0:
#                 negNum+=1
#             total+=nums[i]
#         if k<=negNum:
#             for i in range(k):
#                 total-=2*nums[i]
#         else:
#             for i in range(negNum):
#                 total-=2*nums[i]
#             if (k-negNum)%2!=0:
#                 total-=2*minAbs
#         return total

#发现无论怎么样都是去变最小的那个数，能使总和最大，所以用heap
# class Solution:
#     def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:
#         heapq.heapify(nums)
#         while(k>0):
#             curr=heapq.heappop(nums)
#             curr*=-1
#             heapq.heappush(nums,curr)
#             k-=1
#         return sum(nums)


#2023/05/27
# class Solution:
#     def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:
#         heapq.heapify(nums)
#         while k>0:
#             curr=heapq.heappop(nums)
#             curr*=-1
#             heapq.heappush(nums,curr)
#             k-=1
#         return sum(nums)


#2024/06/01
'''
本质是每一次变动一定是变当前最小的最划算(无论正负)
因为不停要取最小的，所以用heap
'''
# class Solution:
#     def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:
#         heapq.heapify(nums)
#         while k>0:
#             curr=heapq.heappop(nums)
#             curr*=-1
#             heapq.heappush(nums,curr)
#             k-=1
#         return sum(nums)


#2024/09/17
'''
要把这里面的K个数变成负数，求最大的和
首先把k用来把所有的负数变成正数。如果还有多的K,那么就从最小的开始变，只变k%2次

所以需要知道最小的值，所以heap

本质是当前变动都变最小值，无论正负。所以一直变就好了
'''
class Solution:
    def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:
        heapq.heapify(nums)
        while k>0:
            curr=heapq.heappop(nums)
            curr*=-1
            heapq.heappush(nums,curr)
            k-=1
        return sum(nums)
16600