C++  || O(N log N) || sort and map
To get the max sum---> just sort a copy of the array and select last K elements.\nBut while putting those elements into the answer vector, we need to maintain o
To get the max sum---> just sort a copy of the array and select last K elements.\nBut while putting those elements into the answer vector, we need to maintain original array order.\nTo do that we just use a map. \nso finally we have this - \n1) sort a copy of the array.\n2) one by one insert last K elements (of this sorted copy), into the map with the elements frequency.\n\tbasically do ```mp[b[i]]++```.\n3) Iterate over the original aray. If the element is present in map and has non zero frequency, then put it in answer vector. Also, in the map, reduce frequency of that element by 1.\n4) Return answer vector.\n\n\n```\nclass Solution {\npublic:\n    vector<int> maxSubsequence(vector<int>& a, int k) {\n        vector<int>b = a;\n        sort(b.begin(),b.end());\n        int n = a.size();\n        vector<int>ans;\n        unordered_map<int,int>mp;\n        int i=n-1;\n        while(k--){\n            mp[b[i]]++;\n            i--;\n        }\n        for(int i=0;i<n;i++){\n            if(mp.find(a[i])!=mp.end() and mp[a[i]]>0){\n                ans.push_back(a[i]);\n                mp[a[i]]--;\n            }\n        }\n        return ans;\n    }\n};\n```
[]