C++ Backtracking
The problem constraints are too small so we should not be worried about memoisation.\n\nThe challenges of this problem, in my opinion are:\n- Easy to make a mis
The problem constraints are too small so we should not be worried about memoisation.\n\nThe challenges of this problem, in my opinion are:\n- Easy to make a mistake, and it needs some thinking to come up with a clean code.\n- Many edge cases.\n\nLooks like a lot of folks opted for a global variable, which does help reduce code.\n\n**C++**\n```cpp\nvoid dp(vector<int>& tC, int i, int cost, int t, int &best, int max_top = 2) {\n    if (cost >= t || i >= tC.size()) {\n        if (abs(cost - t) < abs(best - t))\n            best = cost;\n        else if (abs(best - t) == abs(cost - t))\n            best = min(best, cost);            \n    }\n    else\n        for (auto j = 0; j <= max_top; ++j)\n            dp(tC, i + 1, cost + j * tC[i], t, best);\n}\nint closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {\n    int best = INT_MAX;\n    for (auto base : baseCosts)\n        dp(toppingCosts, 0, base, target, best);\n    return best;\n}\n```
[]