Java TreeSet solution 6ms
\nclass Solution {\n    public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {\n        TreeSet<Integer> baseSet = new TreeSet(Arrays.stream(
```\nclass Solution {\n    public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {\n        TreeSet<Integer> baseSet = new TreeSet(Arrays.stream(baseCosts).boxed().collect(Collectors.toSet()));\n        if (baseSet.contains(target)) return target;\n        Arrays.sort(toppingCosts);\n        for (int topping: toppingCosts) {\n            for (int base: new ArrayList<>(baseSet)) {\n                int sum = base+topping;\n                baseSet.add(sum);\n                baseSet.add(base+topping*2);\n                if (sum> target) break;\n            }\n            if (baseSet.contains(target)) return target;\n        }\n        Integer left = baseSet.floor(target), right = baseSet.ceiling(target);\n        if (left==null || right==null) return left!=null? left: right;\n        return target-left<=right-target?left: right;\n    }\n}\n```
[]