C++ | priority queue
\ntypedef long long ll;\ntypedef pair<ll, ll> IntPr;\n\nclass Solution {\npublic:\n\n    long long kSum(vector<int>& nums, int k) {\n        ll sum = 0;\n      
```\ntypedef long long ll;\ntypedef pair<ll, ll> IntPr;\n\nclass Solution {\npublic:\n\n    long long kSum(vector<int>& nums, int k) {\n        ll sum = 0;\n        for(auto& val : nums){\n            if(val > 0) sum += val;\n            else val = -val;\n        }\n        if(k == 1) return sum;\n        priority_queue<IntPr> pq;\n        sort(nums.begin(), nums.end());\n        int cnt = 1;\n        pq.push({sum-(ll)nums[0], 0});\n        int n = nums.size();\n      \n        while(pq.empty() == false){\n            ll currVal = pq.top().first, idx = pq.top().second;\n            pq.pop();\n            cnt++;\n            if(cnt == k)\n                return currVal;\n            if(idx < n-1){\n                pq.push({currVal-(ll)nums[idx+1], idx+1});\n                pq.push({currVal+(ll)(nums[idx]-nums[idx+1]), idx+1});\n            }\n        }\n        return 0;\n    }\n};\n```
['C', 'Heap (Priority Queue)']