Greedy Algorithm || C++
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nGreedy Algorithm\n\n# C
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nGreedy Algorithm\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\ntypedef long long ll;\n\nclass Solution {\npublic:\n    long long findMaximumElegance(vector<vector<int>>& items, int k) {\n        ll ans = 0;\n        \n        sort(items.begin(), items.end(), [](const vector<int>& a, const vector<int>& b) {\n            return a[0] > b[0];\n        });\n\n        set<int> uniq_keys;\n        stack<int> seen;\n        \n        \n        ll total = 0;\n        for(int i = 0; i < k; i++) {\n            total += items[i][0];\n            if(uniq_keys.contains(items[i][1]))\n                seen.push(items[i][0]);\n            uniq_keys.insert(items[i][1]);\n        }\n        \n        ans = total + 1LL * uniq_keys.size() * uniq_keys.size();\n        \n        for(int i = k; i < items.size(); i++) {\n            int p = items[i][0];\n            int key = items[i][1];\n            \n            if(!uniq_keys.contains(key) && seen.size() > 0) {\n                total -= seen.top();\n                seen.pop();\n                total += p;\n                uniq_keys.insert(key);\n            }\n            \n            ll tmp = total + 1LL * uniq_keys.size() * uniq_keys.size();\n            ans = max(ans, tmp);\n        }\n        \n        return ans;\n    }\n};\n```
['Hash Table', 'Stack', 'Greedy', 'C++']