public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
res = Arrays.stream(baseCosts).min().getAsInt();
for (int b : baseCosts) {
dfs(toppingCosts, 0, b, target);
public void dfs(int[] toppingCosts, int p, int curCost, int target) {
if (Math.abs(res - target) < curCost - target) {
} else if (Math.abs(res - target) >= Math.abs(curCost - target)) {
if (Math.abs(res - target) > Math.abs(curCost - target)) {
res = curCost;//当前成本更靠近目标
res = Math.min(res, curCost);//成本相同,取较小值
if (p == toppingCosts.length)
dfs(toppingCosts, p + 1, curCost + toppingCosts[p] * 2, target);
dfs(toppingCosts, p + 1, curCost + toppingCosts[p], target);
dfs(toppingCosts, p + 1, curCost, target);