[c++]Priority queue
cpp\n#define ll long long \nclass Solution {\npublic: \n    long long kSum(vector<int>& nums, int k) {\n        ll maxsum=0;\n        for(auto &num : nums){\n  
```cpp\n#define ll long long \nclass Solution {\npublic: \n    long long kSum(vector<int>& nums, int k) {\n        ll maxsum=0;\n        for(auto &num : nums){\n            maxsum+=max(0,num);// adding all pos numbers\n            num = abs(num); // we take abs because for pos numbers we remove from maxsum and negitive numbers decrease maxsum on adding with maxsum so by taking abs we just need to dec num\n        }\n        sort(nums.begin(),nums.end());\n        priority_queue<pair<ll,int>> pq;\n        ll res = maxsum;\n        int curr_k = 1; //maxsum is first largest\n        pq.push({maxsum-nums[0],0});\n        while(curr_k<k){  \n            auto [curr_sum,ind] = pq.top();pq.pop();\n            curr_k++; \n            res = curr_sum; // res contains the curr_k th greatest number\n            if(ind+1<nums.size()){ // gonna remove the next smallest from currsum i.e nums[ind+1]\n                ll include_curr = curr_sum - nums[ind+1];  // we dont remove current ind from maxsum\n                ll exclude_curr = curr_sum + nums[ind] -nums[ind+1]; // we undo the previous removal by adding curr_ind and then remove the next smallest part from maxsum\n                pq.push({include_curr,ind+1});\n                pq.push({exclude_curr,ind+1});\n            }\n            \n        }\n        return res;\n    }\n};\n```
['C', 'Heap (Priority Queue)']