1005. maximize sum of array after k negations
\nclass Solution:\n    def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n        # sort nums according to each num\'s absolute value in desc
```\nclass Solution:\n    def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n        # sort nums according to each num\'s absolute value in descending order\n        nums = sorted(nums, key = abs, reverse = True)\n        \n        # we first negate the smallest negative num (it\'s absolute value is the largest), so it adds most to the sum of nums\n        for i in range(len(nums)):\n            if k > 0 and nums[i] < 0:\n                nums[i] *= -1\n                k -= 1\n        \n        # after negating all negative numbers, if k is still not zero (we need to do more negation), we negate the smallest positive number (which is the last element in nums) k times. Only if k is odd it will be negated \n        if k % 2 == 1:\n            nums[-1] *= -1\n        \n        return sum(nums)\n```
[]