Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 478D Red Green Towers - State Definition

Two parameters

Why can't we use dp[i]dp[i] = "ways to build first ii levels"? Because knowing you've built 5 levels tells you nothing about how many red blocks you've used. Two different colorings might use completely different amounts of red.

You need to track both: dp[i][j]dp[i][j] = "number of ways to build the first ii levels using exactly jj red blocks." Now a state like dp[5][10]dp[5][10] means: "I've built levels 1 through 5, and I've used exactly 10 red blocks so far (out of the rr red blocks available)."