C++ Sorting or Quick Select
See my latest update in repo LeetCode\n## Solution 1. Sorting\n\ncpp\n// OJ: https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/\n/
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n## Solution 1. Sorting\n\n```cpp\n// OJ: https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/\n// Author: github.com/lzl124631x\n// Time: O(NlogN)\n// Space: O(N)\nclass Solution {\npublic:\n    vector<int> maxSubsequence(vector<int>& A, int k) {\n        vector<int> id(A.size());\n        iota(begin(id), end(id), 0); // Index array 0, 1, 2, ...\n        sort(begin(id), end(id), [&](int a, int b) { return A[a] > A[b]; }); // Sort the indexes in descending order of their corresponding values in `A`\n        id.resize(k); // Only keep the first `k` indexes with the greatest `A` values\n        sort(begin(id), end(id)); // Sort indexes in ascending order\n        vector<int> ans;\n        for (int i : id) ans.push_back(A[i]);\n        return ans;\n    }\n};\n```\n\n## Solution 2. Quick Select\n\n```cpp\n// OJ: https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/\n// Author: github.com/lzl124631x\n// Time: O(N + KlogK) on average, O(N^2 + KlogK) in the worst case\n// Space: O(N)\nclass Solution {\npublic:\n    vector<int> maxSubsequence(vector<int>& A, int k) {\n        vector<int> id(A.size());\n        iota(begin(id), end(id), 0);\n        nth_element(begin(id), begin(id) + k, end(id), [&](int a, int b) { return A[a] > A[b]; });\n        id.resize(k);\n        sort(begin(id), end(id));\n        vector<int> ans;\n        for (int i : id) ans.push_back(A[i]);\n        return ans;\n    }\n};\n```\n\n## Solution 3. Quick Select\n\n```cpp\n// OJ: https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/\n// Author: github.com/lzl124631x\n// Time: O(N) on average, O(N^2) in the worst case\n// Space: O(N)\nclass Solution {\npublic:\n    vector<int> maxSubsequence(vector<int>& A, int k) {\n        if (k == A.size()) return A;\n        vector<int> v(begin(A), end(A)), ans;\n        nth_element(begin(v), begin(v) + k - 1, end(v), greater<>());\n        int cnt = count(begin(v), begin(v) + k, v[k - 1]);\n        for (int i = 0; i < A.size(); ++i) {\n            if (A[i] > v[k - 1] || (A[i] == v[k - 1] && --cnt >= 0)) ans.push_back(A[i]);\n        }\n        return ans;\n    }\n};\n```
[]