Java Solution Iterative and Recursive
Since n , m <= 10;\nWe can use brute force.\nThe Time complexity is  O(n*3^m)\n\n\nclass Solution {\n\n    Integer minDiff = Integer.MAX_VALUE;\n    Integer ans
Since n , m <= 10;\nWe can use brute force.\nThe Time complexity is  O(n*3^m)\n\n```\nclass Solution {\n\n    Integer minDiff = Integer.MAX_VALUE;\n    Integer ans = 0;\n\n    public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {\n        for (int baseCost : baseCosts) {\n            dfs(baseCost, 0, toppingCosts, target);\n        }\n        return ans;\n    }\n\n    private void dfs(int cur, int idx, int[] toppingCosts, int target) {\n        if (cur - target >= minDiff) {\n            return;\n        }\n        if (Math.abs(cur - target) <= minDiff) {\n            minDiff = Math.abs(cur - target);\n            ans = cur;\n        }\n        if (idx > toppingCosts.length - 1) {\n            return;\n        }\n        dfs(cur, idx + 1, toppingCosts, target);\n        dfs(cur + toppingCosts[idx], idx + 1, toppingCosts, target);\n        dfs(cur + 2 * toppingCosts[idx], idx + 1, toppingCosts, target);\n        return;\n    }\n}\n```\n\n\n```\nclass Solution {\n    public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {\n        Deque<int[]> dq = new LinkedList<>();\n        int ans = 0;\n        int minDiff = Integer.MAX_VALUE;\n        \n        for (int baseCost : baseCosts) {\n            dq.offer(new int[] {0, baseCost});\n            if (Math.abs(target - baseCost) < minDiff) {\n                minDiff = Math.abs(target - baseCost);\n                ans = baseCost;\n            }\n        }\n        \n        while (!dq.isEmpty()) {\n            int[] cur = dq.pollFirst();\n            int idx = cur[0];\n            if (idx == toppingCosts.length) {\n                break;\n            }\n            int sum = cur[1];\n            int toppingCost = toppingCosts[idx];\n            dq.offer(new int[] {idx+1, sum});\n            \n            for (int i = 0; i < 2; i++) {\n                sum += toppingCost;\n                if (sum - target >= minDiff) {\n                    break;\n                }\n                if (Math.abs(sum - target) <= minDiff) {\n                    ans = sum;\n                    minDiff = Math.abs(sum - target);\n                }\n                dq.offer(new int[] {idx+1, sum});\n            }\n        }\n        return ans;\n    }\n}\n```
[]