[c++  DFS  24ms]  [100%]
The most effient branch cut condition comes from compare 2 slots see if they are equal.\n\n\n\nstruct slot\n{\n    int array[16];\n    int cur = 0;\n    int n =
The most effient branch cut condition comes from compare 2 slots see if they are equal.\n\n\n```\nstruct slot\n{\n    int array[16];\n    int cur = 0;\n    int n = 0;\n    int d = 0;\n    \n    inline bool valid(int v)\n    {\n        if (cur == 0)\n        {\n            return true;\n        }\n        return (array[cur - 1] < v) && (cur < n);\n    }\n\n    inline int push(int v)\n    {\n        int d1 = diff();\n        array[cur] = v;\n        cur++; \n        int d = diff();\n        return d - d1;\n    }\n\n    inline int pop(int v)\n    {\n        int d1 = diff();\n        cur--;\n        int d = diff();\n        return d - d1;\n    }\n\n    inline int diff()\n    {\n        if (cur == 0) return 0;\n        return array[cur - 1] - array[0];\n    }\n    \n    inline bool equal(slot& s)\n    {\n        if (cur != s.cur)\n        {\n            return false;   \n        }\n        if (cur == 0) return true;\n        if (array[0] != s.array[0]) return false;\n        if (array[cur - 1] != s.array[cur - 1]) return false;\n        return true;\n    }\n    \n};\nclass Solution {\npublic:\n    int n;\n    vector<slot> slots;\n    int res = INT_MAX;\n    void visit(vector<int>& nums, int p , int sum)\n    {\n        if (p == n)\n        {        \n            res = min(res, sum);\n        }\n        if (sum >= res)\n        {\n            return;\n        }\n        int v = nums[p];\n        for (int i = 0; i < slots.size(); i++)\n        {\n            if (slots[i].valid(v) )\n            {\n                bool dup = false;\n                for (int j = 0; j < i; j++)\n                {\n                    if (slots[i].equal(slots[j]))\n                    {\n                        dup = true;\n                        break;\n                    }\n                }\n                if (dup)\n                {\n                    continue;\n                }\n                sum += slots[i].push(v);\n                visit(nums, p + 1, sum);\n                sum += slots[i].pop(v);\n            }\n        }\n    }\n    int minimumIncompatibility(vector<int>& nums, int k) {\n        n = nums.size();\n        \n        if (k == n) return 0;\n        slots.resize(k);\n        unordered_map<int, int> m;\n        for (auto& v : nums)\n        {\n            m[v]++;\n        }\n        for (auto& p : m)\n        {\n            if (p.second > k)\n            {\n                return -1;\n            }\n        }\n        \n        for (auto& slot : slots)\n        {\n            slot.n = n / k;\n        }\n        sort(nums.begin(), nums.end());\n        visit(nums, 0, 0);\n        return res == INT_MAX ? -1 : res;\n    }\n};\n```
[]