C++ solution || Priority Queue || With Explanation || Beats 100% runtime and space complexity
The plan is to find the biggest possible sum first, and then go through the array and find the sums of all possible subsequences, from biggest to smallest, unti
The plan is to find the biggest possible sum first, and then go through the array and find the sums of all possible subsequences, from biggest to smallest, until we find the kth sum.\nHere, we start by putting the largest sum in the priority queue and making all of the array\'s elements positive.\nThen, we use this line to find the next sums by taking away the lowest number from the current sum:\n```\npq.push({it.first-nums[it.second], it.second+1});\n```\nAnd at the same time, we use this method to see if we can make a sum by adding back the element\xA0we left out while calculating the current sum:\n```\nif(it.second>0) pq.push({it.first-nums[it.second]+nums[it.second-1], it.second+1});\n```\nThe last thing we do is find the kth largest sum and return it.\xA0Here is how the given logic is implemented effectively:\n```\nclass Solution {\npublic:\n    long long kSum(vector<int>& nums, int k) {\n        priority_queue<pair<long long, int>> pq;\n        long long sum=0;\n        for(int i=0;i<nums.size();i++) {\n            if(nums[i]>0) sum+=nums[i];\n            else nums[i]*=-1;\n        }\n        sort(nums.begin(), nums.end());\n        pq.push({sum, 0});\n        while(k--) {\n            auto it = pq.top();\n            pq.pop();\n            if(it.second<nums.size()) {\n                pq.push({it.first-nums[it.second], it.second+1});\n                if(it.second>0) pq.push({it.first-nums[it.second]+nums[it.second-1], it.second+1});\n            }\n            if(k==0) return it.first;\n        }\n        return -1;\n    }\n};\n```
['C', 'Heap (Priority Queue)']