Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 478D Red Green Towers - Finding the Height

First observation

To find hh, solve h(h+1)2r+g\frac{h(h+1)}{2} \le r + g. You can iterate: start from h=1h = 1 and keep incrementing while h(h+1)2r+g\frac{h(h+1)}{2} \le r + g. The largest such hh is your tower height. Let S=h(h+1)2S = \frac{h(h+1)}{2}.

Now count how many ways to assign colors to each level such that you use at most rr red blocks and at most gg green blocks, with exactly SS total blocks used. This is where the DP comes in.