Hash and qsort to sovle this issue
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\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```\n\nint myCmp(const void *p1, const void *p2) {\n    int *a = *(int **)p1;\n    int *b = *(int **)p2;\n    return b[0] - a[0];\n}\n\ntypedef struct {\n    int key;\n    int val;\n    UT_hash_handle hh;\n} hash_t;\n\nhash_t *findhash(hash_t **obj, int key) {\n    hash_t *tmp = NULL;\n    HASH_FIND_INT(*obj, &key, tmp);\n    return tmp;\n}\n\nvoid addhash(hash_t **obj, int key, int val) {\n    hash_t *tmp = findhash(obj, key);\n    if(tmp == NULL) {\n        tmp = malloc(sizeof(hash_t));\n        tmp->key = key;\n        tmp->val = val;\n        HASH_ADD_INT(*obj, key, tmp);\n    } else {\n        tmp->val = val;\n    }\n}\n\nint gethash(hash_t **obj, int key, int defaultVal) {\n    hash_t *tmp = findhash(obj, key);\n    if(tmp == NULL) {\n        return defaultVal;\n    }\n    return tmp->val;\n}\n\nvoid freehash(hash_t **obj) {\n    hash_t *cur = NULL, *next = NULL;\n    HASH_ITER(hh, *obj, cur, next) {\n        HASH_DEL(*obj, cur);\n        free(cur);\n    }\n}\n\nlong long findMaximumElegance(int** items, int itemsSize, int* itemsColSize, int k){\n    int n = itemsSize;\n    hash_t *obj = NULL;\n    long long ans = 0, total_profit = 0;\n    qsort(items, n, sizeof(int *), myCmp);  //\u5229\u6DA6\u4ECE\u5927\u5230\u5C0F\u6392\u5217\n    int stack[n];\n    int top = 0, val = 0;\n    long long len = 0;\n\n    for(int i = 0; i < n; i++) {\n        int profit = items[i][0], category = items[i][1];\n        if(i < k) {\n            total_profit += profit;    \n            val = gethash(&obj, category, 0);\n            if(val == 0) {\n                addhash(&obj, category, profit);\n                len++;\n            } else {            \n                stack[top++] = profit;\n            }\n\n        } else if((top > 0) && (0 == gethash(&obj, category, 0))) { \n            addhash(&obj, category, profit);\n            len++;\n            total_profit += profit - stack[--top]; \n        }\n        ans = fmax(ans, total_profit + len * len);\n    }\n\n    freehash(&obj);\n    free(obj);\n\n    return ans;\n}\n\n\n\n```
['Hash Table', 'C', 'Sorting']