For selected positions , coverage is equivalent to , , and gaps at most . Let be the minimum cost with last chimney covering houses 1 through . If , ; otherwise . Positive costs make earlier chimneys unnecessary in the first case. The answer is . Time is and space , sufficient for . The small-subtask solution enumerates chimney subsets and combines coverage masks in . Costs and DP values are at most ; use 64-bit.
Solution written by GPT6