C++
\nclass Solution {\n    int n ;\n    int k ;\n    vector<bool>visited ;\n    int ret = INT_MAX ;\n    \n    void dfs(vector<int>&nums, int cur, int count, int l
```\nclass Solution {\n    int n ;\n    int k ;\n    vector<bool>visited ;\n    int ret = INT_MAX ;\n    \n    void dfs(vector<int>&nums, int cur, int count, int low, int high, int sum){\n        if(count == n/k){\n            int i = 0;  \n            while(i < n && visited[i])\n                i++ ;\n            if(i == n){\n                ret = min(ret , sum + high - low) ;\n                return ;\n            }\n            else{\n                visited[i] = true ;\n                dfs(nums, i, 1, nums[i], nums[i], sum + high - low) ;\n                visited[i] = false ;\n            }\n        }\n        else{\n            int last = -1 ;\n            for(int i = cur+1; i < n; i++){\n                if(visited[i])\n                    continue ;\n                if(nums[i] == nums[cur])\n                    continue ;\n                if(nums[i] == last)\n                    continue ;\n                visited[i] = true ;\n                dfs(nums, i, count+1, low, nums[i], sum) ;\n                last = nums[i] ;\n                visited[i] = false ;\n            }\n        }\n    }\npublic:\n    int minimumIncompatibility(vector<int>& nums, int k) {\n        this->n = nums.size() ;\n        this->k = k ;\n        visited.resize(n) ;\n        sort(nums.begin(), nums.end() ) ;\n        int cn = 1 ;\n        \n        for(int i = 1; i < n; i++){\n            if(nums[i] == nums[i-1]){\n                cn++ ;\n                if(cn > k)\n                    return -1 ;\n            }\n            else\n                cn = 1 ;\n        }\n        if(cn > k)\n            return -1 ;\n        visited[0] = true ;\n        dfs(nums, 0, 1, nums[0], nums[0], 0) ;\n        return ret ;\n    }\n};\n```
['Depth-First Search']