Easy Java Solution || Priority Queue || Beats 80%
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n\n\n    // TLE\n\n    // Brute force Approach by finding all the sum ans then sortin\n\n    // static void kSumInArary(int arr[], int x, int idx, ArrayList<Long> curr, PriorityQueue<Long> pq, int k){\n    //     if(idx==arr.length) return;\n    //     if(x==1){\n    //         for(int i=idx; i<arr.length; i++){\n    //             long a = curr.get(0)+(long)arr[i];\n    //             if(pq.size()<k) pq.add(a);\n    //             else{\n    //                 if(a>pq.peek()){\n    //                     pq.remove();\n    //                     pq.add(a);\n    //                 }\n    //             }\n    //         }\n    //         return;\n    //     }\n\n    //     for(int i=idx; i<=arr.length-x; i++){\n    //         curr.set(0,curr.get(0)+(long)arr[i]);\n    //         kSumInArary(arr,x-1,i+1,curr,pq,k);\n    //         curr.set(0,curr.get(0)-(long)arr[i]);\n    //     }\n    // }\n\n    // static int helper(int arr[], int k){\n    //     PriorityQueue<Long> pq = new PriorityQueue<>();\n    //     pq.add((long)0);\n\n    //     int n = arr.length;\n\n\n    //     for(int i=1; i<=n; i++){\n    //         ArrayList<Long> sum = new ArrayList<>();\n    //         sum.add((long)0);\n    //         kSumInArary(arr,i,0,sum,pq,k);\n    //     }\n\n    //     return pq.peek();\n    // }\n\n    public long kSum(int[] nums, int k) {\n        long sum=0L;\n        for (int i = 0; i < nums.length; i++) {\n            if (nums[i]>0)  sum+=nums[i];\n            else    nums[i]=-nums[i];\n        }\n        Arrays.sort(nums);\n        PriorityQueue<Pair<Long,Integer>> pq=new PriorityQueue<>((a,b)->Long.compare(b.getKey(),a.getKey()));\n        pq.offer(new Pair<>(sum,0));\n        while(k-->1){\n            Pair<Long, Integer> top = pq.poll();\n            long s=top.getKey();\n            int i=top.getValue();\n            if(i<nums.length){\n                pq.offer(new Pair<>(s-nums[i],i+1));\n                if(i>0) pq.offer(new Pair<>(s-nums[i]+nums[i-1],i+1));\n            }\n        }\n        return pq.peek().getKey();\n    }\n}\n```
['Java']