When to Use Bitmask DP

n ≤ 20 and tracking used elements

Use bitmask DP when you see these signals:

1.1. Small set size. n20n \le 20 is typical, since 2201062^{20} \approx 10^6.

2.2. Subset tracking. You need to know which items have been used.

3.3. Order or assignment matters. TSP needs to track visited cities; assignment needs to track assigned tasks.

The state is dp[mask]dp[mask] or dp[mask][i]dp[mask][i] where maskmask represents the subset and ii might be the last element.

unnamed - 2026-01-20T063833.036.jpg