    class Solution {

        public int minimumTimeRequired(int[] jobs, int k) {
            List<Integer> list = new ArrayList<>();
            return dfs(0, 0, list, 0, Integer.MAX_VALUE, jobs, k);
        }

        int dfs(int index, int kIndex, List<Integer> list, int max, int ans, int[] jobs, int k) {
            if (kIndex < k) {
                list.add(kIndex, jobs[index]);
                if (Math.max(list.get(kIndex), max) < ans) {
                    if (index == jobs.length - 1) {
                        ans = Math.max(list.get(kIndex), max);
                    } else {
                        ans = dfs(index + 1, kIndex + 1, list, Math.max(list.get(kIndex), max), ans, jobs, k);
                    }
                }
                list.add(kIndex, 0);
            }
            for (int i = 0; i < kIndex; i++) {
                list.set(i, list.get(i) + jobs[index]);
                if (Math.max(list.get(i), max) < ans) {
                    if (index == jobs.length - 1) {
                        ans = Math.max(list.get(i), max);
                    } else {
                        ans = dfs(index + 1, kIndex, list, Math.max(list.get(i), max), ans, jobs, k);
                    }
                }
                list.set(i, list.get(i) - jobs[index]);
            }
            return ans;
        }
    }

//runtime:8 ms
//memory:38.4 MB
