[CPP]: solution with explanation
Based on others suggestions. Check the explanation here: https://math.stackexchange.com/questions/89419/algorithm-wanted-enumerate-all-subsets-of-a-set-in-order
Based on others suggestions. Check the explanation here: https://math.stackexchange.com/questions/89419/algorithm-wanted-enumerate-all-subsets-of-a-set-in-order-of-increasing-sums\n```\nlong long kSum(vector<int>& nums, int k) {\n\tlong long maxsum = 0;\n\tfor (auto& n : nums) {\n\t\tif (n >= 0) maxsum += n;\n\t\telse n = -n;\n\t}\n\tif (k == 1) return maxsum;\n\tsort(nums.begin(), nums.end());\n\tvector<long long> candidates;\n\tusing ip = std::pair<long long, int>;\n\tpriority_queue<ip, vector<ip>, greater<ip>> pq;\n\tpq.push({nums[0], 0});\n\twhile (!pq.empty() && candidates.size() < k - 1) { \n\t\tauto [el, ind] = pq.top(); pq.pop();\n\t\tcandidates.push_back(el);\n\t\tif (ind < nums.size() - 1) {\n\t\t\tpq.push({el + nums[ind + 1], ind + 1});\n\t\t\tpq.push({nums[ind + 1] + (el - nums[ind]), ind + 1});\n\t\t}\n\t}\n\treturn maxsum - candidates[k - 2];\n}\n```
['C++']