C++ | Backtracking with Optimizations
\n\tvoid solve(vector<vector<int>>& sets, vector<int>& nums, int i, int n, int k, int& ans){\n        int sum=0;\n        for(auto set: sets){\n            if(s
```\n\tvoid solve(vector<vector<int>>& sets, vector<int>& nums, int i, int n, int k, int& ans){\n        int sum=0;\n        for(auto set: sets){\n            if(set.size()>=2){\n                sum+=set.back()-set.front();\n            }\n        }\n        if(ans<sum){\n            return;\n        }else if(i==n){\n            ans=min(ans,sum);\n        }else{\n            bool put_in_empty=false;\n            for(auto& set:sets){\n                if(set.size()==0 && !put_in_empty){\n                    set.push_back(nums[i]);\n                    solve(sets,nums,i+1,n,k,ans);\n                    set.pop_back();\n                    put_in_empty=true;\n                }else if(0<set.size() && set.size()*k<n && set.back()<nums[i]){\n                    set.push_back(nums[i]);\n                    solve(sets,nums,i+1,n,k,ans);\n                    set.pop_back();\n                }\n            }\n        }\n    }\n    int minimumIncompatibility(vector<int>& nums, int k) {\n        int n=nums.size();\n        sort(nums.begin(),nums.end());\n        vector<vector<int>> sets(k,vector<int>());\n        int ans=1e9;\n        solve(sets,nums,0,n,k,ans);\n        return ans==1e9?-1:ans;\n    }\n```
[]