[Java] Priority Queue
\n\nclass Solution {\n    public long kSum(int[] nums, int k) {\n        long sum = 0;\n        for(int i=0;i<nums.length;i++){\n            if(nums[i] > 0) sum
\n```\nclass Solution {\n    public long kSum(int[] nums, int k) {\n        long sum = 0;\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        PriorityQueue<long[]> pq = new PriorityQueue(new Comparator<long[]>(){\n            @Override\n            public int compare(long[] a, long[] b){\n                return a[0] - b[0] > 0 ? -1 : 1; \n            }\n        });\n        Arrays.sort(nums);\n        pq.add(new long[]{sum - nums[0], 0});\n        long res = sum;\n        k--;\n        while(k > 0){\n            long[] cur = pq.poll();\n            res = cur[0];\n            if(cur[1] < (long)nums.length - 1) {\n                pq.add(new long[]{cur[0] + nums[(int)cur[1]]- nums[(int)cur[1]+1], cur[1]+1});\n                pq.add(new long[]{cur[0] - nums[(int)cur[1] + 1], cur[1]+1});\n            }\n            k--;\n        }\n        return res;\n    }\n}\n```
['Heap (Priority Queue)', 'Java']