JAVA Time O(N*2^N+3^N) / Space O(2^n)
```\n    static int MAX_VALUE = Integer.MAX_VALUE/2;\n    \n    private int sln1(int[] nums, int k){\n        int n = nums.length;\n        int c = n/k;\n      
```\n    static int MAX_VALUE = Integer.MAX_VALUE/2;\n    \n    private int sln1(int[] nums, int k){\n        int n = nums.length;\n        int c = n/k;\n        int[] map = new int[1<<n];\n\t\t//preprocessing O(n*2^n)\n        for(int i = 0;i< map.length;i++){\n            int max = -1;\n            int min = n+1;\n            if(Integer.bitCount(i) != c) map[i] = MAX_VALUE;\n            else{\n                Set<Integer> set = new HashSet<>();\n                for(int j =0; j< n;j++){\n                    if((i&(1<<j))!=0) {\n                        set.add(nums[j]);\n                        max = Math.max(max, nums[j]);\n                        min = Math.min(min, nums[j]);\n                    }\n                }\n                if(set.size()==c)  map[i] = max-min;\n                else map[i] = MAX_VALUE;\n            }\n        }\n\n        int res = dfs2(k, (1<<n)-1, map, new Integer[1<<n]);\n        if (res >= MAX_VALUE) return -1;\n        else return res;\n    }\n\t\n\t//O(3^N)\n\tprivate int dfs2(int k, int mask, int[] map, Integer[] dp){\n        if(k == 0) return mask==0? 0 :MAX_VALUE;\n        if(dp[mask] != null) return dp[mask];\n        int res = MAX_VALUE;\n        for(int sub = mask; sub>0; sub = (sub-1)&mask){\n            if(map[sub] == MAX_VALUE) continue;\n            res = Math.min(res, dfs2(k-1,  mask^sub, map , dp)+map[sub]);\n        }\n        dp[mask] = res;\n        return res;\n    }
[]