C++ | priority_queue
Got TLEs for enumerating all cases.\nUse this case generator instead:\n1. branch one: including element in index i\n2. branch two: excluding element in index i\
Got TLEs for enumerating all cases.\nUse this case generator instead:\n1. branch one: including element in index i\n2. branch two: excluding element in index i\nwith those two branches, we are able to generate all cases.\n\n```\n#define pls pair<long long, int>\n\nclass Solution {\npublic:\n    long long kSum(vector<int>& nums, int k) {\n        long long sum = 0;\n        int len = nums.size();\n        for (int i = 0; i < len; ++i) {\n            if (nums[i] > 0) {\n                sum += nums[i];\n            }\n            nums[i] = (int)(abs(nums[i]));\n        }\n        auto cmp = [](const pls &lhs, const pls &rhs) {\n            return lhs.first > rhs.first;\n        };\n        sort(nums.begin(), nums.end());\n        priority_queue<pls, vector<pls>, greater<>> que;\n        que.push({nums[0], 0});\n        long long minus = 0;\n        while (--k > 0) {\n            auto front = que.top();\n            que.pop();\n            minus = front.first;\n            if (front.second < len - 1) {\n                que.push({front.first - nums[front.second] + nums[front.second + 1], front.second + 1});\n                que.push({front.first + nums[front.second + 1], front.second + 1});\n            }\n        }\n        return sum - minus;\n    }\n};\n```
[]