Video Solution | Complete Contest | C++ | Java
Intuition, approach, and time complexity discussed in detail in video solution\nhttps://youtu.be/uvF7-rRddzM\n\n# Code\nC++\n\nclass Solution {\npublic:\n    lo
# Intuition, approach, and time complexity discussed in detail in video solution\nhttps://youtu.be/uvF7-rRddzM\n\n# Code\nC++\n```\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        sort(items.begin(), items.end(), [](const vector<int>&a, const vector<int> & b)->bool{\n            return a[0] > b[0];\n        });\n        unordered_set<int> unqCat;\n        vector<int> dupl;\n        long long eleg = 0;\n        long long netProf = 0;\n        for(int indx = 0; indx<items.size(); indx++){\n             int curProf = items[indx][0], curCat = items[indx][1];\n             if(indx < k){\n                if(unqCat.find(curCat) != unqCat.end()){\n                    dupl.push_back(curProf);\n                }\n                netProf += curProf;\n             }else if(unqCat.find(curCat) == unqCat.end()){\n                   if(dupl.size() == 0)break; \n                  //although the k items are already int the bag but increasing the uniquness of the bag will have more positive impact on the eleg of the subseq as compared to decrease in profit by replacing the duplicate item with least profit in the bag which will be present at the end of the bag since we sorted the items accroding to ascending order of the profit.\n                   int profDec = curProf - dupl[dupl.size()-1];\n                   dupl.pop_back();\n                   cout<<profDec<<endl;\n                   netProf += profDec;\n             }\n             unqCat.insert(curCat);\n             int unqCats = unqCat.size();\n             eleg = max(eleg, netProf + 1L * unqCats * unqCats);\n        }\n        return eleg;      \n    }\n};\n```\nJava\n```\nclass Solution {\n    public long findMaximumElegance(int[][] items, int k) {\n        Arrays.sort(items, (a, b)->b[0]-a[0]);\n        Set<Integer> unqCat = new HashSet<>();\n        List<Integer> dupl = new ArrayList<>();\n        long eleg = 0;\n        long netProf = 0;\n        for(int indx = 0; indx<items.length; indx++){\n             int curProf = items[indx][0], curCat = items[indx][1];\n             if(indx < k){\n                if(unqCat.contains(curCat)){\n                    dupl.add(curProf);\n                }\n                netProf += curProf;\n             }else if(!unqCat.contains(curCat)){\n                   if(dupl.size() == 0)break; \n                  //although the k items are already int the bag but increasing the uniquness of the bag will have more positive impact on the eleg of the subseq as compared to decrease in profit by replacing the duplicate item with least profit in the bag which will be present at the end of the bag since we sorted the items accroding to ascending order of the profit.\n                   int profDec = curProf - dupl.remove(dupl.size()-1);\n                //    cout<<profDec<<endl;\n                   netProf += profDec;\n             }\n             unqCat.add(curCat);\n             int unqCats = unqCat.size();\n             eleg = Math.max(eleg, netProf + 1L * unqCats * unqCats);\n        }\n        return eleg;      \n    }\n}\n```
['C++']