Priority Queue | C++
\ntypedef long long ll;\n\nclass Solution {\npublic:\n    long long kSum(vector<int>& nums, int k) {\n\n        int n = nums.size();\n        ll tot = 0;\n     
```\ntypedef long long ll;\n\nclass Solution {\npublic:\n    long long kSum(vector<int>& nums, int k) {\n\n        int n = nums.size();\n        ll tot = 0;\n        for(int i=0;i<n;i++){\n            if(nums[i]>0)\n                tot += nums[i];\n            if(nums[i]<0)\n                nums[i] *= -1;\n        }\n        \n        if(k==1)\n            return tot;\n\n        k--;\n        sort(nums.begin(),nums.end());\n        \n        priority_queue<pair<ll,int>,vector<pair<long long,int>>, greater<pair<long long,int>>>pq;\n        \n        int cur = 0;\n        \n        ll ans = nums[0];\n        \n        pq.push({(ll)nums[0],1});\n        \n        while(cur<=k){\n            cur++;\n            auto a = pq.top();\n            pq.pop();\n            \n            if(cur==k)\n                return tot- a.first;\n            \n            if(a.second != n){\n                \n                ll x = a.first;\n                int y = a.second;\n                x -= nums[y-1];\n                pq.push({x+nums[y],y+1});\n                \n                x += nums[y-1];\n                pq.push({x+nums[y],y+1});\n            }\n        }\n        return 1;\n    }\n};\n```
['C', 'Heap (Priority Queue)']