classSolution:deflargestSumAfterKNegations(self,nums: List[int],k:int) ->int: nums.sort() n =0for i, num inenumerate(nums):if n <= k-1and num <0: nums[i]=-nums[i] n +=1else:breakif k > n:if (k - n) %2==0:returnsum(nums)else: nums.sort() nums[0]=-nums[0]returnsum(nums)else:returnsum(nums)