class Solution:
def closestCost(self,baseCosts, toppingCosts, target):
def dfs(i, cur_cost):
# 更新最接近目标价格的方案
nonlocal res
if abs(cur_cost - target) < abs(res - target) or (abs(cur_cost - target) == abs(res - target) and cur_cost < res):
res = cur_cost
# 达到最后一个配料
if i == len(toppingCosts):
return
# 尝试添加不同数量的配料
for j in range(3):
dfs(i + 1, cur_cost + j * toppingCosts[i])
res = float('inf')
toppingCosts.sort()
for cost in baseCosts:
dfs(0, cost)
return res