This reads like two separate path problems: go from top-left to bottom-right collecting cherries, then come back collecting more. The trips are not independent, though. Cherries picked on the first trip disappear, so the second trip sees a modified grid, and maximizing each trip on its own can lock in a suboptimal total.
Instead of one trip forward and one trip back, model the round trip as two people walking simultaneously from (0, 0) to (n-1, n-1). Person 1 represents the forward trip. Person 2 represents the return trip, reversed so it also runs top-left to bottom-right. Both move one step at a time, either right or down, and a cherry on a cell both occupy is counted once. This reformulation turns a sequential problem with interacting trips into a single DP over both walkers' positions.
1 <= n <= 50 -> The grid is at most 50x50 = 2,500 cells. We have room for O(n^3) or even O(n^4) solutions.grid[i][j] is -1, 0, or 1 -> Thorns block paths entirely, not just increase cost. We must handle unreachable states.Solve the two trips independently: find the path from (0,0) to (n-1,n-1) that collects the most cherries, remove those cherries from the grid, then find a second maximum-cherry path on the modified grid (a return trip is equivalent to another forward trip with the moves reversed).
Each trip on its own is a standard maximum-path-sum DP, so this is easy to implement. It is also wrong. The best single path can take cherries positioned so that whatever remains cannot be covered by one more right/down path, while two coordinated paths, each individually suboptimal, collect more in total. The walkthrough below shows a grid where this happens.
On the walkthrough grid, the greedy split collects 4 cherries while the true maximum is 5. No fix to the first trip repairs this, because the two paths have to be chosen together. The next approach optimizes them as a single decision.
Reversing the return trip makes the two-walker model exact: a path from (n-1,n-1) to (0,0) using left and up moves, read backwards, is a path from (0,0) to (n-1,n-1) using right and down moves, and it visits the same cells. The round trip is therefore two forward paths, chosen together.
Both walkers take exactly 2*(n-1) steps total to reach the destination. At each step, each walker moves either right or down. The number of steps taken so far, call it t, determines a constraint: if a walker is at row r, their column must be c = t - r. So with t fixed, we only need to track r1 (walker 1's row) and r2 (walker 2's row) to know both positions completely.
Cherry collection follows one rule: if both walkers are on the same cell, the cherry is counted once; otherwise each walker collects its own cell. This single rule is enough to prevent any cherry from being counted twice across the whole round trip.
The same-cell check catches every possible double count, not only collisions that happen to occur at the same moment. Any right/down path reaches cell (r, c) after exactly r + c moves. So if both walkers' paths pass through a cell, they occupy it at the same step t = r + c, never at different times. Checking r1 == r2 at each step (with t fixed, equal rows mean equal columns) therefore detects every shared cell.
The reformulation is also exact in the other direction: every pair of forward paths corresponds to a valid round trip and collects the cherries on the union of the two paths, which is precisely the quantity to maximize.
t range from 0 to 2*(n-1). This is the step number (how many moves each walker has made).t, iterate over all valid (r1, r2) pairs where 0 <= r1, r2 <= min(t, n-1) and c1 = t - r1 and c2 = t - r2 are valid column indices.t = 2*(n-1), both walkers are at (n-1, n-1), so the answer is dp[n-1][n-1]. If it is still negative, no valid path pair exists; return 0. (Each step only reads the previous step's values, so the implementation keeps two 2D arrays instead of a full 3D table.)The same recurrence can also be written top down. Recursion with memoization removes the manual loop bounds and buffer swapping, at the cost of an O(n^3) memo table instead of two O(n^2) arrays.
The top-down version asks, from a given pair of positions, what is the best both walkers can still collect on the way to (n-1, n-1). Define a function solve(r1, c1, r2) that returns that maximum. Both walkers have taken the same number of steps, so c2 = r1 + c1 - r2 and does not need to be passed. Each call tries the 4 movement combinations (each walker goes right or down) and takes the maximum over the valid ones.
solve(r1, c1, r2) where c2 = r1 + c1 - r2.r1 == n-1 and c1 == n-1, walker 1 is at the destination, and so is walker 2: the bounds check already passed, and c2 = 2*(n-1) - r2 <= n-1 forces r2 = n-1. Return grid[n-1][n-1], counted once.