Walk Through of my Understanding || Detailed Explaination 💡.
Understanding optimised take or leave :
-> i like to define it as , you should take atleast one element at any point
-> how can we maintain this and get all uni
Understanding optimised take or leave :
        -> i like to define it as , you should take atleast one element at any point
            -> how can we maintain this and get all unique combinations?
        
        instead of starting with a case of not taking any element, we start with "take" case. 1 -take , 0 - leave .

                                    1
                    01                               11
            001             011               101                111 
        0001     0011   0101      0111    1001      1011    1101        1111

        it is like expanding by taking last element each time and leave/take second last element, this will reduce the search space signiificantly as we considering only unique sates at any point.
        in this way we can generate all the unique combinations, where we take atleast one element.
        example :
                    0 1 2 (index)
                    2 2 4

                                     (2) 1 a
                   (2-2+2) 01  b                         (2+2) 11 c
        (2-2+4) 001  d        (2+4) 011      (4-2+4) 101           (4+2) 111   


        2 4 -2
        largest sum = 6
        sort by abs vals (i will explain why at last)
        0 1 2
        2 2 4
        first we have 6 which is 1st largest sum
        * we remove smallest combination sum from that to get second smallest. *
        we use that above tree traversel in greedy way to get next smallest combination sum.
        add the visited states with current sum to min priority queue
        we greedily visit the branches that giving the smallest current sum.
        1st 6 
        2nd 6-2 (a)
        3rd 6-2 (b)
        4th 6-4 (c)
        5th 6-4 (d) => ans = 2
        
        we stop after d as we got k largests sums

        we are getting abs values of nums because adding a negative to val will be same as
        removing positive value from largest sum.
        so instead of dealing with -ve nums , we just make them as +ve.
        and this nums has to sorted in asc order , as we are greedily visting them first.

# Complexity
- Time complexity: N * log(N) (for sorting) + K *log(K) (for k pq operations )
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(k) , at most k values in pq.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public long kSum(int[] nums, int k) {
        long lsum = 0;
        int n = nums.length;
        for(int i =0 ; i<n ; i++){
            if(nums[i]>0) lsum+=nums[i];
            nums[i] = Math.abs(nums[i]);
        }
        Arrays.sort(nums);
        PriorityQueue<long[]> pq = new PriorityQueue<>((a,b)->Long.compare(a[0],b[0])); //(sum,lastIndex)
        long ans = lsum;
        pq.add(new long[]{nums[0],0});
        while(--k>0 && !pq.isEmpty()){
            int lastInd = (int)pq.peek()[1];
            long prevSum = pq.poll()[0];
            ans = lsum - prevSum;
            if(lastInd<n-1){
                pq.add(new long[]{prevSum-nums[lastInd]+nums[lastInd+1],lastInd+1});
                pq.add(new long[]{prevSum+nums[lastInd+1],lastInd+1});
            }
        }
        return ans;
    }
}
/*

    4
    0

    6
    4
    2
    0

    4
    2
    0
    -2

    merge




    -10 -2 1 3 4 12

   considering 12

    12  take
     0  not take

   considering 4
    not take 4
    12          16
     0          12
         merge   4
    take 4       0
    16           
     4

   considering 3
    
    not take 3
     16             
     12             19
      4             15
      0             12
            merge    7
    not take 3       4
     19              3
     15              0
      7
      3

   considering 1
    
    not take 1

    19
    15
    12
     7
     4
     3
     0
            
    20
    16
    13
     8
     5
     4
     1

    20   18
    19   17
    16   14
    15   13
    13   11
    12   10
     8    6
     7    5
     5    3
     4    2
     3    1
     1   -1
     0   -2

-10 -2 1 3 4 12

    20
    19
    18
    18
    17

this gonna be never ending shit


    

*/
```
['Array', 'Sorting', 'Heap (Priority Queue)', 'Java']