<p>You are given an <code>n x n</code> <code>grid</code> representing a field of cherries, each cell is one of three possible integers.</p>

<ul>
	<li><code>0</code> means the cell is empty, so you can pass through,</li>
	<li><code>1</code> means the cell contains a cherry that you can pick up and pass through, or</li>
	<li><code>-1</code> means the cell contains a thorn that blocks your way.</li>
</ul>

<p>Return <em>the maximum number of cherries you can collect by following the rules below</em>:</p>

<ul>
	<li>Starting at the position <code>(0, 0)</code> and reaching <code>(n - 1, n - 1)</code> by moving right or down through valid path cells (cells with value <code>0</code> or <code>1</code>).</li>
	<li>After reaching <code>(n - 1, n - 1)</code>, returning to <code>(0, 0)</code> by moving left or up through valid path cells.</li>
	<li>When passing through a path cell containing a cherry, you pick it up, and the cell becomes an empty cell <code>0</code>.</li>
	<li>If there is no valid path between <code>(0, 0)</code> and <code>(n - 1, n - 1)</code>, then no cherries can be collected.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/grid.jpg" style="width: 242px; height: 242px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,-1],[1,0,-1],[1,1,1]]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The player started at (0, 0) and went down, down, right right to reach (2, 2).
4 cherries were picked up during this single trip, and the matrix becomes [[0,1,-1],[0,0,-1],[0,0,0]].
Then, the player went left, up, up, left to return home, picking up one more cherry.
The total number of cherries picked up is 5, and this is the maximum possible.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1,-1],[1,-1,1],[-1,1,1]]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 50</code></li>
	<li><code>grid[i][j]</code> is <code>-1</code>, <code>0</code>, or <code>1</code>.</li>
	<li><code>grid[0][0] != -1</code></li>
	<li><code>grid[n - 1][n - 1] != -1</code></li>
</ul>

class Solution {
private:
    int solve(int i1, int j1, int i2, vector<vector<int>>& grid, vector<vector<vector<int>>>& dp) {
        int n = grid.size();
        int j2 = i1 + j1 - i2;
        if (i1 >= n || i2 >= n || j1 >= n || j2 >= n || grid[i1][j1] == -1 || grid[i2][j2] == -1) return -2500;
        if (i1 == n-1 && j1 == n-1 && i2 == n-1 && j2 == n-1) return grid[n-1][n-1];

        if (dp[i1][j1][i2] != -1) return dp[i1][j1][i2];

        vector<int> di = {0, 1};
        vector<int> dj = {1, 0};

        int maxi = -2500;

        for (int i = 0; i<2; i++) {
            int newi1 = i1 + di[i];
            int newj1 = j1 + dj[i];
            for (int j = 0; j<2; j++) {
                int newi2 = i2 + di[j];
                int newj2 = j2 + dj[j];
                int val = 0;
                if (i1 == i2 && j1 == j2) val = grid[i1][j1];
                else val = grid[i1][j1] + grid[i2][j2];
                val += solve(newi1, newj1, newi2, grid, dp);
                maxi = max(maxi, val);
            }
        }

        return dp[i1][j1][i2] = maxi;
    }
public:
    int cherryPickup(vector<vector<int>>& grid) {
    int n = grid.size();
    vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>(n, -2500)));
    dp[0][0][0] = grid[0][0];

    for (int i1 = 0; i1 < n; i1++) {
        for (int j1 = 0; j1 < n; j1++) {
            for (int j2 = 0; j2 < n; j2++) {
                int i2 = i1 + j1 - j2;
                if (i2 < 0 || i2 >= n || grid[i1][j1] == -1 || grid[i2][j2] == -1) continue;

                vector<int> di = {-1, 0};
                vector<int> dj = {0, -1};

                int maxCherries = -2500;

                for (int i = 0; i < 2; i++) {
                    int newi1 = i1 + di[i];
                    int newj1 = j1 + dj[i];
                    for (int j = 0; j < 2; j++) {
                        int newi2 = i2 + di[j];
                        int newj2 = j2 + dj[j];
                        if (newi1 >= 0 && newi2 >= 0 && newj1 >= 0 && newj2 >= 0) {
                            maxCherries = max(maxCherries, dp[newi1][newj1][newj2]);
                        }
                    }
                }

                if (maxCherries != -2500) {
                    dp[i1][j1][j2] = maxCherries + grid[i1][j1];
                    if (i1 != i2 || j1 != j2) {
                        dp[i1][j1][j2] += grid[i2][j2];
                    }
                }
            }
        }
    }

    return max(0, dp[n-1][n-1][n-1]);
}

};
83921