Submask DP O(3^N)
\nclass Solution {\npublic:\n    int minimumIncompatibility(std::vector<int>& nums, int k) {\n        int n = nums.size();\n        std::vector<int> dp(1 << n, 
```\nclass Solution {\npublic:\n    int minimumIncompatibility(std::vector<int>& nums, int k) {\n        int n = nums.size();\n        std::vector<int> dp(1 << n, std::numeric_limits<int>::max());\n        dp[0] = 0;\n        std::vector<int> val(1 << n);\n        std::vector<bool> good(1 << n, true);\n        \n        for (int mask = 1; mask < (1 << n); ++mask) {\n            std::set<int> seen;\n            int mn = std::numeric_limits<int>::max();\n            int mx = std::numeric_limits<int>::min();\n            for (int i = 0; i < n; ++i) {\n                if (mask & (1 << i)) {\n                    mn = std::min(mn, nums[i]);\n                    mx = std::max(mx, nums[i]);\n                    if (seen.count(nums[i]) > 0) {\n                        good[mask] = false;\n                    }\n                    seen.insert(nums[i]);\n                }\n            }\n            val[mask] = mx - mn;\n        }\n        \n        for (int mask = 1; mask < (1 << n); ++mask) {\n            int submask = mask;\n            while (submask) {\n                if (k * __builtin_popcount(submask) == n && good[submask] && dp[mask ^ submask] != std::numeric_limits<int>::max()) {\n                    dp[mask] = std::min(dp[mask], val[submask] + dp[mask ^ submask]);\n                }\n                submask = (submask - 1) & mask;\n            }\n        }\n        \n        if (dp[(1 << n) - 1] == std::numeric_limits<int>::max()) {\n            dp[(1 << n) - 1] = -1;\n        }\n        return dp[(1 << n) - 1];\n    }\n};\n```
['C']