C++ | Easy to understand
\nclass Solution {\n    void backtrack(int &cost, int ind, vector<int>& toppingCosts, int &target, int &absDiff, int &ans){\n        if(ind == toppingCosts.size
```\nclass Solution {\n    void backtrack(int &cost, int ind, vector<int>& toppingCosts, int &target, int &absDiff, int &ans){\n        if(ind == toppingCosts.size()){\n            if(abs(target - cost) < absDiff){\n                absDiff = abs(target-cost);\n                ans = cost;\n            }\n            else if(abs(target - cost) == absDiff and cost < target){\n                ans = cost;\n            }\n            return;\n        }\n        for(int i=ind; i<toppingCosts.size(); i++){\n            cost += toppingCosts[i];\n            backtrack(cost, i+1, toppingCosts, target, absDiff, ans);\n            cost += toppingCosts[i];\n            backtrack(cost, i+1, toppingCosts, target, absDiff, ans);\n            cost -= 2*(toppingCosts[i]);\n            backtrack(cost, i+1, toppingCosts, target, absDiff, ans);\n        }\n        return;\n    }\npublic:\n    int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {\n        int absDiff = INT_MAX;\n        int ans = -1;\n        for(int i=0; i<baseCosts.size(); i++){\n            backtrack(baseCosts[i], 0, toppingCosts, target, absDiff, ans);   \n        }\n        return ans;\n    }\n};\n```
['Backtracking']