Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 319C Kalila and Dimna - The DP

State and transition

Let dp[i]dp[i] = minimum cost to reach tree ii. Base: dp[1]=0dp[1] = 0 (start here, no cost). Transition: dp[j]=mini<j(dp[i]+biaj)dp[j] = \min_{i < j}(dp[i] + b_i \cdot a_j). Naive: O(n2)O(n^2). For each jj, try all ii.

But look at the transition: dp[i]+biajdp[i] + b_i \cdot a_j. For fixed jj, rewrite as dp[j]=mini(biaj+dp[i])dp[j] = \min_i(b_i \cdot a_j + dp[i]). This is mini(mixj+ci)\min_i(m_i \cdot x_j + c_i) where mi=bim_i = b_i, ci=dp[i]c_i = dp[i], xj=ajx_j = a_j. That's CHT form!