C++ || BitMask || DP || O(n*2^n)
\nclass Solution {\npublic:\n    int memo[70000];\n    int dp(int mask,vector <int> &comb,map <int ,int> &values){\n        if (mask==0) return 0;\n        if (
```\nclass Solution {\npublic:\n    int memo[70000];\n    int dp(int mask,vector <int> &comb,map <int ,int> &values){\n        if (mask==0) return 0;\n        if (memo[mask]!=-1) return memo[mask];\n        int best=INT_MAX/2;\n        for (auto &c: comb){\n            if ((mask & c)==c){\n                best=min(best,dp(mask ^ c,comb,values)+values[c]);\n            }\n        }\n        return memo[mask]=best;\n    }\n    int minimumIncompatibility(vector<int>& nums, int k) {\n        ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);\n        int n=nums.size();\n        int l=n/k;\n        memset(memo,-1,sizeof(memo));\n        map <int ,int> values;\n        vector <int> comb;\n        for (int mask=0;mask<(1<<n);mask++){\n            int val=0;\n            for (int i=0;i<n;i++){\n                if ((mask & (1<<i))!=0) val++;\n            }\n            if (val==l){\n                bool flag=true;\n                vector <int> seen(n+1,0);\n                vector <int> okay;\n                for (int j=0;j<n;j++){\n                    if ((mask & (1<<j))!=0){\n                        if (seen[nums[j]]==1){\n                            flag=false;\n                            break;\n                        }\n                        seen[nums[j]]=1;\n                        okay.push_back(nums[j]);\n                    } \n                }\n                if (flag){\n                    values[mask]=*max_element(okay.begin(),okay.end())-*min_element(okay.begin(),okay.end());\n                    comb.push_back(mask);\n                }\n            }\n        }\n        int ans=dp((1<<n)-1,comb,values);\n        return (ans<INT_MAX/2)?ans:-1;\n    }\n};\n```
['Dynamic Programming', 'C', 'Bitmask']