Java: Try all combinations
\n\nclass Solution {\n    int ans = Integer.MAX_VALUE;\n    public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {\n        ans = baseCosts[0
```\n\nclass Solution {\n    int ans = Integer.MAX_VALUE;\n    public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {\n        ans = baseCosts[0];\n        for (int base : baseCosts)\n            helper(toppingCosts, target, base, 0);\n        return ans;\n    }\n\n    private void helper(int[] top, int target, int cost, int idx) {\n        if (Math.abs(target - ans) > Math.abs(target - cost)) {\n            ans = cost;\n        } else if (Math.abs(target - ans) == Math.abs(target - cost)) {\n            ans = Math.min(ans, cost);\n        }\n\n        if (idx == top.length) return;\n\n\t\t//either 0, 1 or 2 toppings for each topping\n        for (int i = 0; i <= 2; i++) {\n            helper(top, target, cost + top[idx] * i, idx + 1);\n        }\n    }\n}\n\n```
[]