I'll show you how to speed up the DP using SOS (Sum over Subsets) technique. Instead of iterating all subsets for each mask, you compute values by adding states in a specific order. The core idea: process masks by the number of set bits. For each bit position, add contributions from masks that differ only in that bit. This avoids redundant subset enumeration. You'll see how three nested loops replace exponential subset iteration, turning an O(3^n) solution into O(n × 2^n). The code is short but the speedup is dramatic.
Time complexity: .
Space complexity: iterating over bits.