Dynamic Programming21 sections · 915 units
Open in Course

Commando - Problem Statement

Classic CHT problem

Split nn soldiers into contiguous groups. Group value = a⋅x2+b⋅x+ca \cdot x^2 + b \cdot x + c where xx is sum of strengths in group. dp[i]dp[i] = max value for first ii soldiers.

Transition: dp[i]=max⁡j<i(dp[j]+f(Si−Sj))dp[i] = \max_{j < i}(dp[j] + f(S_i - S_j)) where SS is prefix sum. Expand f(Si−Sj)=a(Si−Sj)2+b(Si−Sj)+cf(S_i - S_j) = a(S_i - S_j)^2 + b(S_i - S_j) + c. Separate terms by ii and jj. Result: linear in SiS_i with slope depending on jj. Apply CHT. Handle max instead of min by negating.