Python, backtrack + memoization
\nimport collections\nclass Solution:\n    def minimumIncompatibility(self, nums: List[int], k: int) -> int:\n        size = len(nums)//k\n        if len(nums)%
```\nimport collections\nclass Solution:\n    def minimumIncompatibility(self, nums: List[int], k: int) -> int:\n        size = len(nums)//k\n        if len(nums)%k!=0:\n            return False\n        cnt = [0]*(len(nums)+1)\n        for i in nums:\n            cnt[i]+=1\n        for i in cnt:\n            if i>k:\n                return -1\n        self.res = inf\n        res = 0\n        memo = {}\n        @lru_cache(None)\n        def dfs(index,k,currsize,currmin,currmax,cnt):\n            if k==0:\n                return 0\n            res = inf\n            for i in range(index,len(nums)+1):\n                currnum = i\n                \n                if cnt[currnum]>0:\n                    newcnt = list(cnt)\n                    newcnt[currnum]-=1\n                    temp1=currmin \n                    temp2= currmax\n                    currmin = min(currmin,currnum)\n                    currmax = max(currmax,currnum)\n                    if currsize+1==size:\n                        res= min(res,currmax-currmin+dfs(0,k-1,0,inf,-inf,tuple(newcnt)))\n                    else:\n                        res = min(res,dfs(i+1,k,currsize+1,currmin,currmax,tuple(newcnt)))\n                    \n                    currmin = temp1\n                    currmax = temp2\n            return res\n        res = dfs(0,k,0,inf,-inf,tuple(cnt))\n        \n        return res\n```
[]