Python Hard
```python3 []
fmin = lambda a, b: a if a < b else b
fmax = lambda a, b: a if a > b else b
class Solution:
    def minimumIncompatibility(self, nums: List[int], k: int) -> int:
        '''
        n <= 16
        2^n

        k <= n

        n % k == 0

        diff between max and min element of each sum

        1 <= nums[i] <= nums.length

        we need to be able to cache easily

        no two equal elements

        maybe bitmask dp, if this tles

        if k == 8, can we brute force?

        '''
        N = len(nums)



        SIZE = N // k

        c = Counter(nums)

        if max(c.values()) > k:
            return -1

        if N == k:
            return 0

        A = [0] * k

        seen = set()
        nums.sort()

        if k == 8 and len(set(nums)) == 16:
            return 8

        @cache
        def get_details(mask):
            mn = 17
            mx = 0
            count = 0
            x = mask


            for i in range(1, N+1):
                if (1 << i) & x:
                    mn = fmin(mn, i)
                    mx = fmax(mx, i)
                    count += 1


            return mx - mn, count



        def calc(index, A):
           
            if tuple(sorted(A)) in seen:
                return 10000

            seen.add(tuple(sorted(A)))
           
            if index == N:
    
                total = 0

                for x in A:

                    # mn = 17
                    # mx = 0


                    # for i in range(1, N+1):
                    #     if (1 << i) & x:
                    #         mn = fmin(mn, i)
                    #         mx = fmax(mx, i)
                   
                    total += get_details(x)[0]

                return total

            
            y = nums[index]
            best = 100000

            # B = [0] * k

            # for index, x in enumerate(A): 
            #     B[index] = x



            for i, x in enumerate(A):
                if not ((1 << y) & x):
                    # count = 0
                    # if x != 0:
                    #     for j in range(1, N+1):
                    #         if x & (1 << j):
                    #             count += 1
                    #             if count == SIZE:
                    #                 break

                    if get_details(x)[1] < SIZE:
                        A[i] |= (1 << y)
                        best = fmin(best, calc(index + 1, A))
                        A[i] ^= (1 << y)


            return best




        val = calc(0, A) 

        if val < 10000:
            return val

        return -1



        
```
['Python3']