Skip to content

Chapter 4: Dynamic Programming

Overview

DP is the single most important paradigm in competitive programming. If you could only learn one advanced technique, this would be it. The core idea is simple — avoid recomputing overlapping subproblems — but the art is in designing the right state and transition. This chapter builds from basic 1D recurrences to bitmask DP, digit DP, and tree DP, then goes deep into optimization techniques (Convex Hull Trick, Knuth's, Slope Trick) that separate Expert from Master on Codeforces.

Coming from: Chapter 3 — Graph Foundations — BFS, DFS, shortest paths, and MST algorithms
Going to: Chapter 5 — Strings — string hashing, KMP, suffix arrays, and pattern matching
Bridge: BRIDGE-to-advanced.md — transitioning from DP mastery to advanced topics

Topics at a Glance

#TopicDifficultyKey Trigger Phrase
01DP Thinking FrameworkBeginner"How do I design a DP state?"
02DP — 1D IntroductionBeginner"Fibonacci-like recurrence" or "count ways"
03DP — KnapsackIntermediate"Select items with weight limit to maximize value"
042D / Sequence DPIntermediate"LIS, LCS, edit distance" — compare two sequences
05DP — IntervalIntermediate"Merge stones" or "optimal split of range [l, r]"
06DP — BitmaskIntermediaten ≤ 20 + "assign/use elements from a set" (TSP)
07DP — DigitAdvanced"Count integers in [L, R] with digit property"
08DP on TreesIntermediate"Subtree aggregation" or "optimal coloring on a tree"
09SOS DPAdvanced"Sum over all subsets" or "for each mask, aggregate submasks"
10DP — Convex Hull TrickAdvancedDP recurrence of form dp[i] = min(dp[j] + b[j]·a[i])
11DP — D&C OptimizationAdvanced"Optimal split point is monotone" in row transitions
12Slope TrickAdvanced"Piecewise-linear convex cost function"
13Knuth's OptimizationAdvanced"Interval DP with quadrangle inequality"
14Aliens Trick (WQS Binary Search)Advanced"Choose exactly k items" + convex cost → Lagrangian relaxation

If You Only Read 3 Files

  1. DP Thinking Framework — because knowing how to design a DP matters more than memorizing specific DPs. This file teaches you to identify states, transitions, and base cases systematically.
  2. DP — Bitmask — because bitmask DP is the gateway to a whole class of problems (TSP, assignment, subset optimization) and trains you to think about state compression. Appears constantly at CF 1600+.
  3. DP on Trees — because tree DP combines graph thinking with DP thinking, and tree problems are among the most common on Codeforces. Understanding subtree aggregation unlocks dozens of problem types.

Cross-Chapter Connections


File Listing

FileTopicLevel
dp-thinking-frameworkDP Thinking Framework
dp-intro-1dDynamic Programming — 1D Introduction
dp-2d-sequence2D / Sequence DP (LIS, LCS, Edit Distance)⭐⭐
dp-knapsackDP — Knapsack⭐⭐
dp-on-treesDP on Trees⭐⭐
dp-intervalDP — Interval⭐⭐
dp-bitmaskDP — Bitmask⭐⭐
dp-digitDP — Digit⭐⭐⭐
dp-sosSOS DP (Sum Over Subsets)⭐⭐⭐
dp-divide-and-conquer-optimizationDP — Divide and Conquer Optimization⭐⭐⭐
dp-convex-hull-trickDP — Convex Hull Trick⭐⭐⭐
dp-knuth-optimizationKnuth's Optimization⭐⭐⭐
dp-slope-trickSlope Trick⭐⭐⭐
aliens-trickAliens Trick (WQS Binary Search)⭐⭐⭐

Suggested Reading Order

  1. dp-thinking-framework — how to design states, transitions, and optimizations
  2. dp-intro-1d — overlapping subproblems and memoization basics
  3. dp-2d-sequence — LCS, LIS, and Edit Distance
  4. dp-knapsack — the archetypal DP problem with weight/value tradeoffs
  5. dp-on-trees — subtree DP with post-order traversal
  6. dp-interval — optimize over contiguous range splits
  7. dp-bitmask — subset enumeration when n ≤ 20
  8. dp-digit — count integers with digit-level constraints
  9. dp-sos — sum over all subsets via Zeta transform
  10. dp-divide-and-conquer-optimization — exploit monotone split points
  11. dp-convex-hull-trick — maintain envelope of linear functions
  12. dp-knuth-optimization — reduce interval DP via quadrangle inequality
  13. dp-slope-trick — optimize convex piecewise-linear DPs
  14. aliens-trick — remove "exactly k" constraint via Lagrangian relaxation

Built for the climb from Codeforces 1100 to 2100.