[Java] Submasks O(n * 3^m)
https://cp-algorithms.com/algebra/all-submasks.html\n\n\n    public int closestCostSubmasks(int[] baseCosts, int[] toppingCosts, int target) {\n        int res 
https://cp-algorithms.com/algebra/all-submasks.html\n\n```\n    public int closestCostSubmasks(int[] baseCosts, int[] toppingCosts, int target) {\n        int res = (int) 1e9;\n        int closest = (int) 1e9;\n        final int m = toppingCosts.length;\n        final int[] costs = new int[1 << m];\n        for (int mask = 0; mask < 1 << m; mask++) {\n            int curr = 0;\n            for (int j = 0; j < m; j++) {\n                if ((mask & (1 << j)) != 0) {\n                    curr += toppingCosts[j];\n                }\n            }\n            costs[mask] = curr;\n        }\n        for (int b : baseCosts) {\n            for (int mask = 0; mask < (1 << toppingCosts.length); mask++) {\n                final int abs = Math.abs(b + costs[mask] - target);\n                if (abs < closest) {\n                    closest = abs;\n                    res = b + costs[mask];\n                } else if (abs == closest) {\n                    if (b + costs[mask] < res) {\n                        res = b + costs[mask];\n                    }\n                }\n                for (int subMask = mask; subMask > 0; subMask = (subMask - 1) & mask) {\n                    for (int i = 0; i < toppingCosts.length; i++) {\n                        final int curr = b + costs[mask] + costs[subMask];\n                        if (Math.abs(curr - target) < closest) {\n                            closest = Math.abs(curr - target);\n                            res = curr;\n                        } else if (Math.abs(curr - target) == closest) {\n                            if (curr < res) {\n                                res = curr;\n                            }\n                        }\n                    }\n                }\n            }\n        }\n        return res;\n    }\n```\n\nAlternatively generate all subsets O(n * 2^(2*m)) = O(n * 4^m)\n```\n    public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {\n        final int n = toppingCosts.length * 2;\n        final int[] masks = new int[1 << n];\n        for (int mask = 0; mask < (1 << n); mask++) {\n            int cost = 0;\n            for (int i = 0; i < n; i++) {\n                if ((mask & (1 << i)) != 0) {\n                    cost += toppingCosts[i % toppingCosts.length];\n                }\n            }\n            masks[mask] = cost;\n        }\n        int res = (int) 1e9;\n        int closest = (int) 1e9;\n        for (int b : baseCosts) {\n            for (int mask : masks) {\n                final int abs = Math.abs(b + mask - target);\n                if (abs < closest) {\n                    closest = abs;\n                    res = b + mask;\n                } else if (abs == closest) {\n                    if (b + mask < res) {\n                        res = b + mask;\n                    }\n                }\n            }\n        }\n        return res;\n    }\n```
[]