C++ solution based on provided hints
\nclass Solution {\npublic:\n  long long findMaximumElegance(vector<vector<int>>& items, long long k) {\n    long long n = items.size();\n    sort(items.begin()
```\nclass Solution {\npublic:\n  long long findMaximumElegance(vector<vector<int>>& items, long long k) {\n    long long n = items.size();\n    sort(items.begin(), items.end(), [] (const auto& a, const auto& b) {\n      return a[0] > b[0];\n    });\n    long long totProf = 0;\n    int lastMultipleIdx = -1;\n    unordered_map<int, int> seen;\n    for (int i = 0; i < k; ++i) {\n      totProf += items[i][0];\n      if (seen[items[i][1]]++ > 0) {\n        lastMultipleIdx = i;\n      }\n    }\n    long long maxTot = totProf + ((long long)seen.size() * (long long)seen.size());\n    if (lastMultipleIdx == -1) return maxTot;\n    for (int i = k; i < n; ++i) {\n      if (!seen.count(items[i][1])) {\n        long long tot = totProf - (items[lastMultipleIdx][0] - items[i][0]) +\n          ((long long)(seen.size() + 1) * (long long)(seen.size() + 1));\n        if (tot > maxTot) {\n          maxTot = tot;\n        }\n        totProf -= (items[lastMultipleIdx][0] - items[i][0]);\n        //maxTot = tot;\n        seen[items[i][1]]++;\n        seen[items[lastMultipleIdx][1]]--;\n        //swap(items[i], items[lastMultipleIdx]);\n        bool foundNew = false;\n        for (int j = lastMultipleIdx - 1; j >= 0; --j) {\n          if (seen[items[j][1]] > 1) {\n            lastMultipleIdx = j;\n            foundNew = true;\n            break;\n          }\n        }\n        if (!foundNew) break;\n      }\n    }\n    int firstSingleIdx = -1;\n    return maxTot;\n  }\n};\n```
[]