3ms || 88 Beats 🍏|| Priority Queue 😎
ApproachUse a Min-Heap (PriorityQueue) to always negate the smallest element, updating the sum accordingly for K operations.Complexity
Time complexity: O(n + k
# Approach
Use a Min-Heap (PriorityQueue) to always negate the smallest element, updating the sum accordingly for K operations.
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: O(n + k log n) → O(n) for heap construction, O(k log n) for K extractions and insertions.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n) → For storing n elements in the PriorityQueue.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
![a497d0b1-b512-40b1-9bca-4cbcaeb6befd_1709339631.914102.png](https://assets.leetcode.com/users/images/71c6eedd-2952-40fa-b965-83df6ebe65ec_1739553703.797193.png)

# Code
```java []
class Solution {
    public int largestSumAfterKNegations(int[] nums, int k) {
        int count=0;
       PriorityQueue<Integer> pq=new PriorityQueue<>();
       for(int num:nums){
        pq.add(num);
        count+=num;
       } 
       while(k>0){
        int curmin=pq.poll();
        count-=curmin;
        int operation = curmin * -1;
        count+=operation;
        pq.add(operation);
        k--;
       }

       return count;
    }
}
```
['Array', 'Heap (Priority Queue)', 'Java']