def can_assign(tasks, T, k): tasks.sort(reverse=True) n = len(tasks) groups = [] def backtrack(index): if index == n: return True current = tasks[index] # Try adding to existing groups for i in range(len(groups)): # Skip groups with the same capacity as previous to avoid duplicates if i > 0 and groups[i] == groups[i-1]: continue if groups[i] + current <= T: # Save the state before modification prev_groups = groups.copy() groups[i] += current # Sort to maintain descending order and avoid duplicates groups.sort(reverse=True) if backtrack(index + 1): return True # Restore the previous state groups[:] = prev_groups # Try creating a new group if possible if len(groups) < k: prev_groups = groups.copy() groups.append(current) groups.sort(reverse=True) if backtrack(index + 1): return True # Restore the previous state groups[:] = prev_groups return False return backtrack(0) def main(): import sys input = sys.stdin.read().split() ptr = 0 T = int(input[ptr]) ptr += 1 N = int(input[ptr]) ptr += 1 tasks = [] for _ in range(N): tasks.append(int(input[ptr])) ptr += 1 # Check if any task exceeds T for t in tasks: if t > T: print(-1) return # Iterate possible k from 1 to N for k in range(1, N+1): if can_assign(tasks.copy(), T, k): print(k) return print(N) # All tasks need separate groups if __name__ == "__main__": main()