Python 3 solutions - without using heap data structure and with heaps
This solution is quite tasking to account for all the edge cases.  I did see shorter solutions using heaps which I don\'t fully understand yet but hope to learn
This solution is quite tasking to account for all the edge cases.  I did see shorter solutions using heaps which I don\'t fully understand yet but hope to learn in the near future.\n# Code\n```python\nclass Solution:\n    def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:        \n        nums.sort()\n        negative = sorted(x for x in nums if x < 0)\n        len_neg = len(negative)\n        positive = sorted(x for x in nums if x > 0)\n        if 0 in nums:\n            if not negative:\n                return sum(positive)\n            else:\n                if k >= len_neg:\n                    return sum(positive) - sum(negative)\n                return sum(positive) - sum(negative[:k]) + sum(negative[k:])\n        else:            \n            if not negative:\n                if k % 2:\n                    return sum(nums) - 2 * nums[0]\n                return sum(nums)\n            else:\n                if k >= len_neg:\n                    if (k - len_neg) % 2:\n                        return sum(positive) - sum(negative) - 2 * min(-negative[-1], positive[0] if positive else float(\'inf\'))\n                    return sum(positive) - sum(negative)\n                if k < len_neg:\n                    return sum(positive) - sum(negative[:k]) + sum(negative[k:])            \n```\n\nFinally learnt how to use heaps. :)\n```py\nclass Solution:\n    def largestSumAfterKNegations(self, nums: List[int], k: int) -> int:\n        heapify(nums)\n        for i in range(k):\n            heappush(nums, -heappop(nums))\n        return sum(nums)\n```
['Python3']