Java | Backtracking | Explained
\nclass Solution {\n    int cost=Integer.MAX_VALUE;\n    public int closestCost(int[] bc, int[] tc, int target) {\n\n        for(int i=0; i<bc.length; i++){\n  
```\nclass Solution {\n    int cost=Integer.MAX_VALUE;\n    public int closestCost(int[] bc, int[] tc, int target) {\n\n        for(int i=0; i<bc.length; i++){\n            helper(bc[i], tc, target, new int[tc.length], 0);  // consider every base cost \n        }\n        \n        return cost;\n    }\n    \n    private void helper(int currCost, int[] tc, int target, int[] vis, int idx){\n      // If diff between Cost which I already have > the currcost  OR  Cost(exisiting result) == CurrCost and CurrCost is less than Global Cost then update result\n        if(Math.abs(target-cost) > Math.abs(target-currCost) || (Math.abs(target-cost) == Math.abs(target-currCost) && currCost < cost)) \n            cost = currCost;\n        \n        \n        for(int i=idx; i<tc.length; i++){\n            if(vis[i]==2 ) continue;  // can take atmost 2 toppings\n            vis[i]+=1; // mark topping as taken\n            helper(currCost+tc[i], tc, target, vis, i); // backtrack\n            vis[i]-=1; // unmark topping as not taken\n        }\n    }\n}\n```\n**Upvote if you like the Solution**
['Backtracking', 'Java']