Hello, Codeforces!
Introduction
Searching for editorials for this contest can be surprisingly annoying, since there is no official editorial on the AtCoder website.
Because of this, my friends (murkat and GuilhermeKK) and I felt it was necessary to put together a unified guide all in one place. We essentially wanted to create the editorial we wish we had when we were first solving these problems!
Who is this for?
This contest covers all the fundamental DP techniques and optimizations. If you are not familiar with DP (and you definitely should be, since it is one of the most common topics in cp), the first few problems will be perfect for you to understand the reasoning behind it.
If you know the basics of DP but feel like you are always one step away from finding the right state or transition, there are a lot of great problems here for you to practice. If you are looking for DP optimizations, the most important ones are all covered.
If you already handle complex transition and know the classic optimizations, a quick skim through the final problems can still be a great way to recap and solidify your DP skills.
Hints
When learning a new technique, spending time with some problems is necessary. Because of that, it doesn't make much sense to jump straight into the full editorial without putting in some effort first.
If you are stuck, consider reading these hints before going to the full solutions.
Problem A — Frog 1
Hint 1Think about a DP state for every $$$i$$$.
Hint 2Let $$$DP[i]$$$ be the minimum cost to reach stone $$$i$$$. What are the answer, base case and transition?
Hint 3If the frog is currently at stone $$$i$$$, where could it have jumped from? According to the rules, it must have come from either stone $$$i-1$$$ or stone $$$i-2$$$. How can you use $$$DP[i-1]$$$ and $$$DP[i-2]$$$ to calculate $$$DP[i]$$$?
Problem B — Frog 2
Hint 1Instead of just checking stones $$$i-1$$$ and $$$i-2$$$, the frog can come from any of the previous $$$K$$$ stones. How can you use the previous DP states to calculate $$$DP[i]$$$?
Problem C — Vacation
Hint 1Think about a DP state for every $$$i$$$. Does it have enough information?
Hint 2If we just define $$$DP[i]$$$ as the maximum happiness on day $$$i$$$, we don't know which activity we did on day $$$i$$$, so we can't guarantee we won't repeat it on day $$$i+1$$$. What can we store to solve this? Try expanding your state with a second parameter.
Hint 3Let $$$DP[i][j]$$$ be the maximum happiness on day $$$i$$$, using activity $$$j$$$ on day $$$i$$$. What are the answer, base case and transition?
Hint 4If we use activity $$$j$$$ on day $$$i$$$, then on day $$$i-1$$$ we can't use activity $$$j$$$, so we have to use one of the other activities.
Problem D — Knapsack 1
Hint 1Think about a DP state for every $$$i$$$. Does it have enough information?
Hint 2If we just define $$$DP[i]$$$ as the maximum value with items up to $$$i$$$, we don't know how much capacity we have left, so we don't know which items we are allowed to take. What can we store to solve this? Try expanding your state with a second parameter.
Hint 3Let $$$DP[i][j]$$$ be the maximum value with items up to $$$i$$$, with capacity $$$j$$$. What are the answer, base case and transition?
Hint 4If we are at the item $$$i$$$, we have two options: take the item or leave it. Note that we can only take the item if we have enough capacity. How can we use the previous DP states to calculate the current state?
Problem E — Knapsack 2
Hint 1If you try to use the exact same state from Knapsack 1, you will get MLE. Can you think on another way to write our state?
Hint 2Notice that the maximum possible value is small ($$$N \le 100$$$ and $$$V_i \le 10^3$$$, so the maximum total value is $$$10^5$$$).
Hint 3Let $$$DP[i][j]$$$ be the minimum capacity required to get value exactly $$$j$$$ with items up to $$$i$$$. What are the answer, base case and transition?
Hint 4If we are at the item $$$i$$$, we have two options: take the item or leave it. How can we use the previous DP states to calculate the current state?
Problem F — LCS
Hint 1Think about comparing prefixes.
Hint 2Let $$$DP[i][j]$$$ be the length of the LCS using the first $$$i$$$ characters of $$$S$$$ and the first $$$j$$$ characters of $$$T$$$. What are the answer, base case and transition?
Problem G — Longest Path
Hint 1Since this is a Directed Acyclic Graph (DAG), we can store information about a node $$$v$$$ that depends on its neighbors, without worrying about infinite loops.
Hint 2Let $$$DP[v]$$$ be the longest path starting at vertex $$$v$$$. What are the answer, base case and transition?
Problem H — Grid 1
Hint 1To reach any cell $$$(i, j)$$$, you must have come from either the cell directly above it or the cell directly to its left.
Hint 2Let $$$DP[i][j]$$$ be the number of ways to get to cell $$$(i, j)$$$ starting from cell $$$(1, 1)$$$. What are the answer, base case and transition?
Problem I — Coins
Hint 1We want to find the probability of getting more heads than tails, which means we need a specific number of heads. Try thinking about a $$$N^2$$$ approach.
Hint 2Let $$$DP[i][j]$$$ be the probability of getting exactly $$$j$$$ heads using the first $$$i$$$ coins. What are the answer, base case and transition?
Problem J — Sushi
Hint 1We can just count how many dishes have exactly 1, 2 or 3 pieces of sushi.
Hint 2Define $$$dp[i][j][k]$$$ as the expected number of operations remaining when there are $$$i$$$, $$$j$$$, and $$$k$$$ dishes with 1, 2, and 3 sushi.
Hint 3$$$ E[\text{current state}] = \sum P(\text{next state}) \times (1 + E[\text{next state}]) $$$ Hint 4Manipulate the formula for $$$dp[i][j][k]$$$ to have it isolated on one side. You can now compute it recursively.
Problem K — Stones
Hint 1Winning and Losing states.
Hint 2Calculate winning and losing states backwards, saving them in a dp.
Problem L — Deque
Hint 1Note that both Taro and Jiro play optimally to maximize their own score minus the other's score. This means that at each turn each player will play the move that maximizes the score.
Hint 2Since players remove elements from the edges, the remaining numbers always form a contiguous subarray. Think about DP states over intervals.
Hint 3Let $$$DP[i][j]$$$ be the maximum value you can get if the game is played in the interval $$$[i, j]$$$. What are the answer, base case and transition?
Problem M — Candies
Hint 1Let $$$DP[i][j]$$$ be the number of ways to distribute exactly $$$j$$$ candies among the first $$$i$$$ children. What are the answer, base case and transition?
Hint 2Try to optimize the naive transition.
Problem N — Slimes
Hint 1Since we can only combine adjacent slimes, a combined slime always represents a contiguous interval of the original array. Think about the contiguous intervals.
Hint 2Let $$$DP[i][j]$$$ be the minimum cost required to combine all slimes in the interval $$$[i, j]$$$. What are the answer, base case and transition?
Problem O — Matching
Hint 1The constraint is $$$N \le 21$$$, think about bitmasks.
Hint 2Let $$$DP[i][mask]$$$ be the number of ways to match $$$i$$$ men with the women available in $$$mask$$$. What are the answer, base case and transition?
Hint 3We do not need both parameters. If our state is just the mask of available women, how do we know which men we are currently pairing?
Problem P — Independent Set
Hint 1Think about a DP state for every subtree rooted at $$$v$$$.
Hint 2If we define $$$DP[v]$$$ as the number of ways to color the subtree of $$$v$$$, we run into a problem: we don't know the color of $$$v$$$, so we don't know how to color its parent. What can we store to solve this? Try expanding your state with a second parameter.
Hint 3Let $$$DP[v][0]$$$ be the number of ways to color the subtree of $$$v$$$ if we color the node $$$v$$$ white and $$$DP[v][1]$$$ be the number of ways to color the subtree of $$$v$$$ if we color the node $$$v$$$ black. What are the answer, base case and transition?
Problem Q — Flowers
Hint 1Try to think of a $$$N^2$$$ solution.
Hint 2Let $$$DP[x]$$$ be the maximum sum if the last flower we take has height $$$x$$$. Calculate $$$DP[x]$$$ iterating through flowers $$$i$$$ from left to right. What are the answer, base case and transition?
Hint 3For each flower $$$i$$$, we can take it or not. If we take it, then we use the maximum value $$$v$$$ in $$$DP[1..h[i]]$$$ and set $$$DP[h[i]] = v + a[i]$$$. How can we optimize this transition?
Hint 4Use Segment trees to optimize the transition.
Problem R — Walk
Hint 1The standard $$$DP[k][v]$$$ takes $$$\mathcal{O}(V^2 K)$$$. We need a faster way to compute transitions.
Hint 2Think about how we count paths of length 2 using the adjacency matrix $$$A$$$.
Problem S — Digit Sum
Hint 1We must build the number digit by digit.
Hint 2Our state needs to track the number of digits placed and the current sum modulo $$$D$$$. We also need a boolean flag.
Problem T — Permutation
Hint 1Instead of trying to guess the exact absolute values of the permutation, build the sequence by tracking the relative rank .
Problem U — Grouping
Hint 1Let $$$DP[mask]$$$ be the max score to partition the rabbits in the mask. You can iterate over all submasks to form a new group.
Hint 2How can you avoid redundant calculations? Try fixing one specific element (like the lowest set bit in your mask) and forcing it to be part of the new group you are creating.
Problem V — Subtree
Hint 1Doing a standard Tree DP for every possible root takes $$$\mathcal{O}(N^2)$$$, which is too slow.
Hint 2You need Rerooting DP. This technique uses two DFS passes. The first calculates the answers for subtrees going "down". The second DFS calculates the valid components strictly "above" the current subtree.
Problem W — Intervals
Hint 1If $$$DP[i]$$$ is the max score ending with a '1' at position $$$i$$$, the naive $$$\mathcal{O}(N^2)$$$ transition looks for the best previous '1' at position $$$j$$$.
Hint 2How to optimize it using Segment Tree? You will need lazy propagation.
Problem X — Tower
Hint 1Is there a better order to construct a tower using a specific set of blocks?
Hint 2Think about Exchange Argument.
Problem Y — Grid 2
Hint 1The grid is massive ($$$10^5 \times 10^5$$$), but there are very few walls ($$$N \le 3000$$$).
Hint 2Try to find out a quadratic DP int the number of walls.
Problem Z — Frog 3
Hint 1Manipulate the formula for transitions.
Hint 2You will need Convex Hull trick.
The Full Editorial
The complete solutions and detailed explanations can be found in this PDF: Editorial_Atcoder_Educational_DP_contest.pdf. Once again, we highly encourage you to only read the full solution for a problem after you have genuinely struggled with it and checked the hints!
Conclusion
Writing this guide was really fun, and we put a lot of effort into making it as clear as possible. If you liked it, have any suggestions, or even if you found any mistakes, please let us know in the comments!
We would love to know if you found this useful, as we are currently planning to write more editorials for ICPC-style contests that lack official solutions.