You are an expert Python programmer. Write correct, efficient Python 3 code that solves the problem and passes all tests. Output the code in the following format: ```python
<code>
```

PROBLEM:
You are given a 2D matrix grid of size m x n. You are also given a non-negative integer k.
Return the number of submatrices of grid that satisfy the following conditions:

The maximum element in the submatrix less than or equal to k.
Each row in the submatrix is sorted in non-increasing order.

A submatrix (x1, y1, x2, y2) is a matrix that forms by choosing all cells grid[x][y] where x1 <= x <= x2 and y1 <= y <= y2.

Example 1:

Input: grid = [[4,3,2,1],[8,7,6,1]], k = 3
Output: 8
Explanation:

The 8 submatrices are:

[[1]]
[[1]]
[[2,1]]
[[3,2,1]]
[[1],[1]]
[[2]]
[[3]]
[[3,2]]

Example 2:

Input: grid = [[1,1,1],[1,1,1],[1,1,1]], k = 1
Output: 36
Explanation:
There are 36 submatrices of grid. All submatrices have their maximum element equal to 1.

Example 3:

Input: grid = [[1]], k = 1
Output: 1

Constraints:

1 <= m == grid.length <= 103
1 <= n == grid[i].length <= 103
1 <= grid[i][j] <= 109
1 <= k <= 109

Your solution to the problem should be a method of the class Solution called countSubmatrices and should pass all tests. Use the following starter code:
```python
class Solution:
    def countSubmatrices(self, grid: List[List[int]], k: int) -> int:
        # Add your code here
```

SOLUTION:

Question:
PROBLEM:
You are given a 2D matrix grid of size m x n. You are also given a non-negative integer k.
Return the number of submatrices of grid that satisfy the following conditions:

The maximum element in the submatrix less than or equal to k.
Each row in the submatrix is sorted in non-increasing order.

A submatrix (x1, y1, x2, y2) is a matrix that forms by choosing all cells grid[x][y] where x1 <= x <= x2 and y1 <= y <= y2.

Example 1:

Input: grid = [[4,3,2,1],[8,7,6,1]], k = 3
Output: 8
Explanation:

The 8 submatrices are:

[[1]]
[[1]]
[[2,1]]
[[3,2,1]]
[[1],[1]]
[[2]]
[[3]]
[[3,2]]

Example 2:

Input: grid = [[1,1,1],[1,1,1],[1,1,1]], k = 1
Output: 36
Explanation:
There are 36 submatrices of grid. All submatrices have their maximum element equal to 1.

Example 3:

Input: grid = [[1]], k = 1
Output: 1

Constraints:

1 <= m == grid.length <= 103
1 <= n == grid[i].length <= 103
1 <= grid[i][j] <= 109
1 <= k <= 109

Your solution to the problem should be a method of the class Solution called countSubmatrices and should pass all tests. Use the following starter code:
```python
class Solution:
    def countSubmatrices(self, grid: List[List[int]], k: int) -> int:
        # Add your code here
```

SOLUTION:

assert Solution().countSubmatrices(grid = [[4,3,2,1],[8,7,6,1]], k = 3) == 8

assert Solution().countSubmatrices(grid = [[1]], k = 1) == 1

assert Solution().countSubmatrices(grid = [[2,1],[3,2]], k = 2) == 5

assert Solution().countSubmatrices(grid = [[2,1,1],[1,1,1]], k = 2) == 18

Answer: assert Solution().countSubmatrices(grid = [[2,1,1],[1,1,1],[1,1,1]], k = 2) == 36

Answer: assert Solution().countSubmatrices(grid = [[4,3,2,1],[8,7,6,1]], k = 3) == 8

Answer: assert Solution().countSubmatrices(grid = [[1]], k = 1) == 1

Answer: assert Solution().countSubmatrices(grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9) == 18

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6],[5,4,3,2,1]], k = 5) == 15

Answer: assert Solution().countSubmatrices(grid = [[5,4,3,2,1],[10,9,8,7,6],[15,14,13,12,11]], k = 5) == 15

Answer: assert Solution().countSubmatrices(grid = [[3,3,3,3],[3,3,3,3],[3,3,3,3]], k = 3) == 60

Answer: assert Solution().countSubmatrices(grid = [[5,5,5],[5,5,5],[5,5,5]], k = 5) == 36

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6],[6,5,4,3],[3,2,1,0]], k = 5) == 22

Answer: assert Solution().countSubmatrices(grid = [[1,1,1],[1,1,1],[1,1,1]], k = 1) == 36

Answer: assert Solution().countSubmatrices(grid = [[5,4,3],[3,3,3],[2,2,2]], k = 3) == 21

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7],[6,5,4,3],[2,1,0,-1]], k = 5) == 22

Answer: assert Solution().countSubmatrices(grid = [[2,2,2,2],[2,2,2,2],[2,2,2,2]], k = 2) == 60

Answer: assert Solution().countSubmatrices(grid = [[2,1],[3,2]], k = 2) == 5

Answer: assert Solution().countSubmatrices(grid = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]], k = 1) == 100

Answer: assert Solution().countSubmatrices(grid = [[10,9,8],[7,6,5],[4,3,2]], k = 10) == 36

Answer: assert Solution().countSubmatrices(grid = [[2,1,1],[1,1,1]], k = 2) == 18

Answer: assert Solution().countSubmatrices(grid = [[9,9,9],[9,9,9],[9,9,9]], k = 8) == 0

Answer: assert Solution().countSubmatrices(grid = [[10,9,8],[7,6,5],[4,3,2],[1,0,0]], k = 5) == 21

Answer: assert Solution().countSubmatrices(grid = [[3,2,1],[3,2,1],[3,2,1]], k = 3) == 36

Answer: assert Solution().countSubmatrices(grid = [[5,6,7],[4,5,6],[3,4,5]], k = 5) == 10

Answer: assert Solution().countSubmatrices(grid = [[3,2,1],[2,1,0],[1,0,0]], k = 3) == 36

Answer: assert Solution().countSubmatrices(grid = [[9,8,7],[6,5,4],[3,2,1]], k = 10) == 36

Answer: assert Solution().countSubmatrices(grid = [[2,2,2],[2,2,2],[2,2,2]], k = 2) == 36

Answer: assert Solution().countSubmatrices(grid = [[3,2,1],[2,2,2],[1,2,3]], k = 2) == 17

Answer: assert Solution().countSubmatrices(grid = [[5,4,3],[4,3,2],[3,2,1]], k = 3) == 15

Answer: assert Solution().countSubmatrices(grid = [[1,2,3],[3,2,1],[2,3,1],[1,2,3]], k = 2) == 12

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6,5,4,3,2,1],[9,8,7,6,5,4,3,2,1,0],[8,7,6,5,4,3,2,1,0,-1],[7,6,5,4,3,2,1,0,-1,-2],[6,5,4,3,2,1,0,-1,-2,-3]], k = 6) == 470

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5],[5,4,3,2,1],[2,3,4,5,6],[6,5,4,3,2],[3,4,5,6,7]], k = 5) == 67

Answer: assert Solution().countSubmatrices(grid = [[100,99,98,97,96,95,94,93,92,91],[90,89,88,87,86,85,84,83,82,81],[80,79,78,77,76,75,74,73,72,71],[70,69,68,67,66,65,64,63,62,61]], k = 80) == 165

Answer: assert Solution().countSubmatrices(grid = [[7,6,5,4,3,2,1],[6,5,4,3,2,1,0],[5,4,3,2,1,0,-1]], k = 4) == 81

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5,6,7,8,9,10],[10,9,8,7,6,5,4,3,2,1],[1,2,3,4,5,6,7,8,9,10],[10,9,8,7,6,5,4,3,2,1],[1,2,3,4,5,6,7,8,9,10],[10,9,8,7,6,5,4,3,2,1],[1,2,3,4,5,6,7,8,9,10],[10,9,8,7,6,5,4,3,2,1],[1,2,3,4,5,6,7,8,9,10],[10,9,8,7,6,5,4,3,2,1]], k = 5) == 100

Answer: assert Solution().countSubmatrices(grid = [[5,4,3,2,1],[4,3,2,1,0],[3,2,1,0,-1],[2,1,0,-1,-2],[1,0,-1,-2,-3]], k = 2) == 114

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]], k = 2) == 30

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6,5,4,3,2,1],[20,19,18,17,16,15,14,13,12,11],[30,29,28,27,26,25,24,23,22,21],[40,39,38,37,36,35,34,33,32,31],[50,49,48,47,46,45,44,43,42,41]], k = 30) == 330

Answer: assert Solution().countSubmatrices(grid = [[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1]], k = 1) == 330

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6,5,4,3,2,1],[8,7,6,5,4,3,2,1,0],[7,6,5,4,3,2,1,0,-1]], k = 5) == 115

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5,6,7,8,9,10],[10,9,8,7,6,5,4,3,2,1],[9,8,7,6,5,4,3,2,1,0],[8,7,6,5,4,3,2,1,0,-1],[7,6,5,4,3,2,1,0,-1,-2]], k = 5) == 220

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7],[4,5,6,7,8],[5,6,7,8,9]], k = 5) == 35

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5,6,7,8,9,10],[9,8,7,6,5,4,3,2,1,0],[10,9,8,7,6,5,4,3,2,1]], k = 5) == 57

Answer: assert Solution().countSubmatrices(grid = [[9,7,5,3,1],[8,6,4,2,0],[7,5,3,1,-1],[6,4,2,0,-2],[5,3,1,-1,-3]], k = 5) == 119

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6,5],[8,7,6,5,4],[7,6,5,4,3],[6,5,4,3,2],[5,4,3,2,1]], k = 5) == 70

Answer: assert Solution().countSubmatrices(grid = [[10,20,30,40,50,60,70,80,90,100],[100,90,80,70,60,50,40,30,20,10],[10,20,30,40,50,60,70,80,90,100],[100,90,80,70,60,50,40,30,20,10]], k = 50) == 40

Answer: assert Solution().countSubmatrices(grid = [[5,5,5,5,5,5,5,5,5,5],[5,5,5,5,5,5,5,5,5,5],[5,5,5,5,5,5,5,5,5,5],[5,5,5,5,5,5,5,5,5,5]], k = 5) == 550

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5,6,7,8,9],[2,3,4,5,6,7,8,9,10],[3,4,5,6,7,8,9,10,11],[4,5,6,7,8,9,10,11,12],[5,6,7,8,9,10,11,12,13]], k = 5) == 35

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6,5,4,3,2,1,0],[8,7,6,5,4,3,2,1,0,-1],[7,6,5,4,3,2,1,0,-1,-2],[6,5,4,3,2,1,0,-1,-2,-3]], k = 5) == 285

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6,5,4,3,2,1],[9,8,7,6,5,4,3,2,1,10],[8,7,6,5,4,3,2,1,10,9],[7,6,5,4,3,2,1,10,9,8]], k = 6) == 155

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]], k = 3) == 45

Answer: assert Solution().countSubmatrices(grid = [[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1]], k = 1) == 1155

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5,6,7,8,9],[9,8,7,6,5,4,3,2,1],[2,3,4,5,6,7,8,9,10],[10,9,8,7,6,5,4,3,2]], k = 5) == 35

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6,5,4,3,2,1],[8,7,6,5,4,3,2,1,0],[7,6,5,4,3,2,1,0,-1],[6,5,4,3,2,1,0,-1,-2],[5,4,3,2,1,0,-1,-2,-3]], k = 3) == 185

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6,5,4,3,2,1],[20,19,18,17,16,15,14,13,12,11],[30,29,28,27,26,25,24,23,22,21],[40,39,38,37,36,35,34,33,32,31],[50,49,48,47,46,45,44,43,42,41]], k = 25) == 210

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5],[5,4,3,2,1],[4,5,4,3,2],[3,4,5,4,3],[2,3,4,5,4]], k = 4) == 55

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10]], k = 1) == 15

Answer: assert Solution().countSubmatrices(grid = [[100,99,98,97,96],[95,94,93,92,91],[90,89,88,87,86],[85,84,83,82,81],[80,79,78,77,76]], k = 90) == 90

Answer: assert Solution().countSubmatrices(grid = [[10,9,8],[7,6,5],[4,3,2],[1,0,-1],[-1,-2,-3],[-2,-3,-4]], k = 5) == 65

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5],[5,4,3,2,1],[1,3,2,5,4],[4,5,1,3,2]], k = 3) == 23

Answer: assert Solution().countSubmatrices(grid = [[100,90,80,70,60],[95,85,75,65,55],[90,80,70,60,50],[85,75,65,55,45],[80,70,60,50,40]], k = 75) == 87

Answer: assert Solution().countSubmatrices(grid = [[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5]], k = 5) == 225

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]], k = 5) == 50

Answer: assert Solution().countSubmatrices(grid = [[1,1,1,1,1,1,1,1,1,1],[2,2,2,2,2,2,2,2,2,2],[3,3,3,3,3,3,3,3,3,3],[4,4,4,4,4,4,4,4,4,4],[5,5,5,5,5,5,5,5,5,5]], k = 3) == 330

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6,5,4,3,2,1],[8,7,6,5,4,3,2,1,0],[7,6,5,4,3,2,1,0,0],[6,5,4,3,2,1,0,0,0],[5,4,3,2,1,0,0,0,0]], k = 4) == 265

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6,5,4,3,2,1],[1,9,8,7,6,5,4,3,2],[2,1,9,8,7,6,5,4,3]], k = 5) == 58

Answer: assert Solution().countSubmatrices(grid = [[7,6,5,4,3,2,1],[6,6,6,6,6,6,6],[5,5,5,5,5,5,5],[4,4,4,4,4,4,4],[3,3,3,3,3,3,3],[2,2,2,2,2,2,2],[1,1,1,1,1,1,1]], k = 4) == 290

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5,6,7,8,9,10],[10,9,8,7,6,5,4,3,2,1],[1,1,1,1,1,1,1,1,1,1]], k = 5) == 90

Answer: assert Solution().countSubmatrices(grid = [[5,5,5,5,5,5,5,5,5,5],[5,5,5,5,5,5,5,5,5,5],[5,5,5,5,5,5,5,5,5,5]], k = 4) == 0

Answer: assert Solution().countSubmatrices(grid = [[6,5,4,3,2,1],[5,5,4,3,2,1],[4,4,4,3,2,1],[3,3,3,3,2,1]], k = 4) == 133

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6,5],[8,7,6,5,4],[7,6,5,4,3],[6,5,4,3,2],[5,4,3,2,1]], k = 6) == 114

Answer: assert Solution().countSubmatrices(grid = [[1,10,9,8,7],[2,9,8,7,6],[3,8,7,6,5],[4,7,6,5,4],[5,6,5,4,3]], k = 5) == 30

Answer: assert Solution().countSubmatrices(grid = [[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1]], k = 1) == 550

Answer: assert Solution().countSubmatrices(grid = [[3,2,1,0],[-1,-2,-3,-4],[0,-1,-2,-3],[1,0,-1,-2]], k = 2) == 84

Answer: assert Solution().countSubmatrices(grid = [[1,1,1,1,1],[1,2,2,2,1],[1,2,3,2,1],[1,2,2,2,1],[1,1,1,1,1]], k = 2) == 119

Answer: assert Solution().countSubmatrices(grid = [[7,6,5,4,3,2,1,0,-1,-2],[6,5,4,3,2,1,0,-1,-2,-3],[5,4,3,2,1,0,-1,-2,-3,-4]], k = 4) == 201

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5],[5,4,3,2,1],[4,3,2,1,0],[3,2,1,0,-1],[2,1,0,-1,-2]], k = 2) == 67

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6,5,4,3,2,1],[8,7,6,5,4,3,2,1,0],[7,6,5,4,3,2,1,0,-1],[6,5,4,3,2,1,0,-1,-2]], k = 5) == 215

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6],[9,8,7,6,5],[8,7,6,5,4],[7,6,5,4,3],[6,5,4,3,2]], k = 7) == 114

Answer: assert Solution().countSubmatrices(grid = [[7,6,5,4,3,2,1],[6,5,4,3,2,1,0],[5,4,3,2,1,0,-1],[4,3,2,1,0,-1,-2],[3,2,1,0,-1,-2,-3]], k = 3) == 185

Answer: assert Solution().countSubmatrices(grid = [[7,6,5,4,3],[6,5,4,3,2],[5,4,3,2,1],[4,3,2,1,0]], k = 4) == 65

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6],[5,4,3,2,1],[6,5,4,3,2],[7,6,5,4,3],[8,7,6,5,4]], k = 5) == 65

Answer: assert Solution().countSubmatrices(grid = [[7,6,5,4],[7,6,5,4],[7,6,5,4],[7,6,5,4]], k = 4) == 10

Answer: assert Solution().countSubmatrices(grid = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]], k = 10) == 22

Answer: assert Solution().countSubmatrices(grid = [[3,3,3,3,3,3],[3,3,3,3,3,3],[3,3,3,3,3,3],[3,3,3,3,3,3]], k = 3) == 210

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6,5,4,3,2,1],[9,8,7,6,5,4,3,2,1,0],[8,7,6,5,4,3,2,1,0,-1],[7,6,5,4,3,2,1,0,-1,-2]], k = 5) == 215

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6,5,4,3,2,1],[9,8,7,6,5,4,3,2,1,0],[8,7,6,5,4,3,2,1,0,0],[7,6,5,4,3,2,1,0,0,0],[6,5,4,3,2,1,0,0,0,0],[5,4,3,2,1,0,0,0,0,0],[4,3,2,1,0,0,0,0,0,0],[3,2,1,0,0,0,0,0,0,0],[2,1,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0]], k = 1) == 715

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],[15,14,13,12,11,10,9,8,7,6,5,4,3,2,1],[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]], k = 7) == 41

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7],[4,5,6,7,8],[5,6,7,8,9]], k = 4) == 20

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6],[6,5,4,3],[3,2,1,0],[0,-1,-2,-3],[-3,-4,-5,-6]], k = 5) == 84

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5],[5,4,3,2,1],[2,3,4,5,6],[6,5,4,3,2],[3,4,5,6,7]], k = 4) == 35

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6,5,4,3,2,1],[9,8,7,6,5,4,3,2,1,1],[8,7,6,5,4,3,2,1,1,1],[7,6,5,4,3,2,1,1,1,1],[6,5,4,3,2,1,1,1,1,1]], k = 3) == 185

Answer: assert Solution().countSubmatrices(grid = [[5,4,3,2,1],[4,4,3,2,1],[3,3,3,2,1],[2,2,2,2,1],[1,1,1,1,1]], k = 3) == 144

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5,6,7,8,9,10],[2,1,3,4,5,6,7,8,9,10],[3,2,1,4,5,6,7,8,9,10],[4,3,2,1,5,6,7,8,9,10],[5,4,3,2,1,6,7,8,9,10],[6,5,4,3,2,1,7,8,9,10],[7,6,5,4,3,2,1,8,9,10],[8,7,6,5,4,3,2,1,9,10],[9,8,7,6,5,4,3,2,1,10],[10,9,8,7,6,5,4,3,2,1]], k = 5) == 315

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6,5,4,3,2,1],[9,8,7,6,5,4,3,2,1,0],[8,7,6,5,4,3,2,1,0,0],[7,6,5,4,3,2,1,0,0,0]], k = 5) == 215

Answer: assert Solution().countSubmatrices(grid = [[1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1,1]], k = 1) == 4356

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6,5,4,3,2,1,0],[8,7,6,5,4,3,2,1,0,0],[7,6,5,4,3,2,1,0,0,0],[6,5,4,3,2,1,0,0,0,0],[5,4,3,2,1,0,0,0,0,0]], k = 4) == 360

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6,5,4,3,2,1],[9,8,7,6,5,4,3,2,1,10],[8,7,6,5,4,3,2,1,10,9],[7,6,5,4,3,2,1,10,9,8],[6,5,4,3,2,1,10,9,8,7]], k = 6) == 210

Answer: assert Solution().countSubmatrices(grid = [[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5]], k = 4) == 0

Answer: assert Solution().countSubmatrices(grid = [[1,2,3,4,5,6,7,8,9],[9,8,7,6,5,4,3,2,1],[1,2,3,4,5,6,7,8,9],[9,8,7,6,5,4,3,2,1],[1,2,3,4,5,6,7,8,9]], k = 5) == 55

Answer: assert Solution().countSubmatrices(grid = [[3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3],[3,3,3,3,3,3,3,3,3,3]], k = 2) == 0

Answer: assert Solution().countSubmatrices(grid = [[7,6,5,4,3,2,1],[7,6,5,4,3,2,1],[7,6,5,4,3,2,1]], k = 4) == 60

Answer: assert Solution().countSubmatrices(grid = [[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1,1]], k = 1) == 825

Answer: assert Solution().countSubmatrices(grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]], k = 1) == 225

Answer: assert Solution().countSubmatrices(grid = [[10,20,30,40,50],[50,40,30,20,10],[10,10,10,10,10],[20,30,40,50,60],[30,20,10,10,10]], k = 20) == 35

Answer: assert Solution().countSubmatrices(grid = [[9,8,7,6,5,4,3,2,1],[8,8,7,6,5,4,3,2,1],[7,7,7,6,5,4,3,2,1],[6,6,6,6,5,4,3,2,1],[5,5,5,5,5,4,3,2,1]], k = 5) == 255

Answer: assert Solution().countSubmatrices(grid = [[10,9,8,7,6,5,4,3,2,1],[9,8,7,6,5,4,3,2,1,0],[8,7,6,5,4,3,2,1,0,-1]], k = 5) == 115

import random
import functools
import collections
import string
import math
import datetime

from typing import *
from functools import *
from collections import *
from itertools import *
from heapq import *
from bisect import *
from string import *
from operator import *
from math import *

inf = float('inf')

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def list_node(values: list):
    if not values:
        return None
    head = ListNode(values[0])
    p = head
    for val in values[1:]:
        node = ListNode(val)
        p.next = node
        p = node
    return head

def is_same_list(p1, p2):
    if p1 is None and p2 is None:
        return True
    if not p1 or not p2:
        return False
    return p1.val == p2.val and is_same_list(p1.next, p2.next)

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def tree_node(values: list):
    if not values:
        return None
    root = TreeNode(values[0])
    i = 1
    queue = deque()
    queue.append(root)
    while queue:
        node = queue.popleft()
        if i < len(values) and values[i] is not None:
            node.left = TreeNode(values[i])
            queue.append(node.left)
        i += 1
        if i < len(values) and values[i] is not None:
            node.right = TreeNode(values[i])
            queue.append(node.right)
        i += 1
    return root

def is_same_tree(p, q):
    if not p and not q:
        return True
    elif not p or not q:
        return False
    elif p.val != q.val:
        return False
    else:
        return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)

from dataclasses import dataclass

@dataclass(frozen=True)
class T:
  subarrayWidth: int
  rowIndex: int
  accumulatedSubmatrices: int

class Solution:
  def countSubmatrices(self, grid: list[list[int]], k: int) -> int:
    m = len(grid)
    n = len(grid[0])
    ans = 0
    # dp[i][j] := the number of valid subarrays ending in grid[i][j]
    dp = [[0] * n for _ in range(m)]
    # stacks[j] := the stack of valid
    # (subarray width, row index, number of accumulated submatrices) ending in
    # column j
    stacks: list[T] = [[T(0, -1, 0)] for _ in range(n)]

    for i, row in enumerate(grid):
      for j, num in enumerate(row):
        if num > k:
          stacks[j] = [T(0, i, 0)]
        else:
          dp[i][j] = 1
          if j > 0 and row[j - 1] <= k and row[j - 1] >= row[j]:
            # Extend the valid subarrays to the current number.
            dp[i][j] += dp[i][j - 1]
          width = dp[i][j]
          # Remove subarray widths greater than the current count since they
          # will become invalid.
          while stacks[j] and width < stacks[j][-1].subarrayWidth:
            stacks[j].pop()
          height = i - stacks[j][-1].rowIndex
          newSubmatrices = width * height
          accumulatedSubmatrices = (stacks[j][-1].accumulatedSubmatrices +
                                    newSubmatrices)
          ans += accumulatedSubmatrices
          stacks[j].append(T(width, i, accumulatedSubmatrices))

    return ans


You are an expert Python programmer. Write correct, efficient Python 3 code that solves the problem and passes all tests. Output the code in the following format: ```python
<code>
```

PROBLEM:
You are given two strings s and t of the same length, and two integer arrays nextCost and previousCost.
In one operation, you can pick any index i of s, and perform either one of the following actions:

Shift s[i] to the next letter in the alphabet. If s[i] == 'z', you should replace it with 'a'. This operation costs nextCost[j] where j is the index of s[i] in the alphabet.
Shift s[i] to the previous letter in the alphabet. If s[i] == 'a', you should replace it with 'z'. This operation costs previousCost[j] where j is the index of s[i] in the alphabet.

The shift distance is the minimum total cost of operations required to transform s into t.
Return the shift distance from s to t.

Example 1:

Input: s = "abab", t = "baba", nextCost = [100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [1,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Output: 2
Explanation:

We choose index i = 0 and shift s[0] 25 times to the previous character for a total cost of 1.
We choose index i = 1 and shift s[1] 25 times to the next character for a total cost of 0.
We choose index i = 2 and shift s[2] 25 times to the previous character for a total cost of 1.
We choose index i = 3 and shift s[3] 25 times to the next character for a total cost of 0.

Example 2:

Input: s = "leet", t = "code", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Output: 31
Explanation:

We choose index i = 0 and shift s[0] 9 times to the previous character for a total cost of 9.
We choose index i = 1 and shift s[1] 10 times to the next character for a total cost of 10.
We choose index i = 2 and shift s[2] 1 time to the previous character for a total cost of 1.
We choose index i = 3 and shift s[3] 11 times to the next character for a total cost of 11.

Constraints:

1 <= s.length == t.length <= 105
s and t consist only of lowercase English letters.
nextCost.length == previousCost.length == 26
0 <= nextCost[i], previousCost[i] <= 109

Your solution to the problem should be a method of the class Solution called shiftDistance and should pass all tests. Use the following starter code:
```python
class Solution:
    def shiftDistance(self, s: str, t: str, nextCost: List[int], previousCost: List[int]) -> int:
        # Add your code here
```

SOLUTION:

Question:
PROBLEM:
You are given two strings s and t of the same length, and two integer arrays nextCost and previousCost.
In one operation, you can pick any index i of s, and perform either one of the following actions:

Shift s[i] to the next letter in the alphabet. If s[i] == 'z', you should replace it with 'a'. This operation costs nextCost[j] where j is the index of s[i] in the alphabet.
Shift s[i] to the previous letter in the alphabet. If s[i] == 'a', you should replace it with 'z'. This operation costs previousCost[j] where j is the index of s[i] in the alphabet.

The shift distance is the minimum total cost of operations required to transform s into t.
Return the shift distance from s to t.

Example 1:

Input: s = "abab", t = "baba", nextCost = [100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [1,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Output: 2
Explanation:

We choose index i = 0 and shift s[0] 25 times to the previous character for a total cost of 1.
We choose index i = 1 and shift s[1] 25 times to the next character for a total cost of 0.
We choose index i = 2 and shift s[2] 25 times to the previous character for a total cost of 1.
We choose index i = 3 and shift s[3] 25 times to the next character for a total cost of 0.

Example 2:

Input: s = "leet", t = "code", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Output: 31
Explanation:

We choose index i = 0 and shift s[0] 9 times to the previous character for a total cost of 9.
We choose index i = 1 and shift s[1] 10 times to the next character for a total cost of 10.
We choose index i = 2 and shift s[2] 1 time to the previous character for a total cost of 1.
We choose index i = 3 and shift s[3] 11 times to the next character for a total cost of 11.

Constraints:

1 <= s.length == t.length <= 105
s and t consist only of lowercase English letters.
nextCost.length == previousCost.length == 26
0 <= nextCost[i], previousCost[i] <= 109

Your solution to the problem should be a method of the class Solution called shiftDistance and should pass all tests. Use the following starter code:
```python
class Solution:
    def shiftDistance(self, s: str, t: str, nextCost: List[int], previousCost: List[int]) -> int:
        # Add your code here
```

SOLUTION:

assert Solution().shiftDistance(s = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", t = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 50

Answer: assert Solution().shiftDistance(s = "abab", t = "baba", nextCost = [100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [1,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) == 2

Answer: assert Solution().shiftDistance(s = "xyz", t = "abc", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 9

Answer: assert Solution().shiftDistance(s = "abcd", t = "abcd", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 0

Answer: assert Solution().shiftDistance(s = "zzzz", t = "aaaa", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 4

Answer: assert Solution().shiftDistance(s = "abcdefghijklmnopqrstuvwxyz", t = "zyxwvutsrqponmlkjihgfedcba", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 2366

Answer: assert Solution().shiftDistance(s = "abcd", t = "dcba", nextCost = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) == 0

Answer: assert Solution().shiftDistance(s = "aabbcc", t = "zzxxww", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 322

Answer: assert Solution().shiftDistance(s = "abcde", t = "bcdea", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 104

Answer: assert Solution().shiftDistance(s = "leet", t = "code", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 31

Answer: assert Solution().shiftDistance(s = "aaaa", t = "zzzz", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 4

Answer: assert Solution().shiftDistance(s = "abc", t = "bcd", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 6

Answer: assert Solution().shiftDistance(s = "zzzz", t = "aaaa", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 104

Answer: assert Solution().shiftDistance(s = "aabbcc", t = "zzxxww", nextCost = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], previousCost = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]) == 272

Answer: assert Solution().shiftDistance(s = "zzzzzzzzzz", t = "aaaaaaaaaa", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 10

Answer: assert Solution().shiftDistance(s = "abcdefgabcdefg", t = "ghijklmghijklm", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 84

Answer: assert Solution().shiftDistance(s = "abcdefg", t = "gfedcba", nextCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1], previousCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]) == 336

Answer: assert Solution().shiftDistance(s = "hello", t = "world", nextCost = [9,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9]) == 42

Answer: assert Solution().shiftDistance(s = "zzzyyy", t = "aaaxxx", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 6

Answer: assert Solution().shiftDistance(s = "aabbccdd", t = "zzxxwwvv", nextCost = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], previousCost = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) == 38

Answer: assert Solution().shiftDistance(s = "shift", t = "right", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 6

Answer: assert Solution().shiftDistance(s = "abcabcabcabc", t = "xyzxyzxyzxyz", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 624

Answer: assert Solution().shiftDistance(s = "abacabadabacaba", t = "zbyczbxcbyczbx", nextCost = [100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [1,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) == 7

Answer: assert Solution().shiftDistance(s = "abcdefghijklmnopqrstuvwxyza", t = "bcdefghijklmnopqrstuvwxyzab", nextCost = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26], previousCost = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) == 352

Answer: assert Solution().shiftDistance(s = "abcdef", t = "fedcba", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 234

Answer: assert Solution().shiftDistance(s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", t = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 1300

Answer: assert Solution().shiftDistance(s = "mississippi", t = "ssissippiim", nextCost = [3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3]) == 74

Answer: assert Solution().shiftDistance(s = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", t = "zyxwvutsrqponmlkjihgfedcbazyxwvutsrqponmlkjihgfedcba", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 4732

Answer: assert Solution().shiftDistance(s = "programming", t = "software", nextCost = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], previousCost = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) == 46

Answer: assert Solution().shiftDistance(s = "leet", t = "teel", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 208

Answer: assert Solution().shiftDistance(s = "hello", t = "ollhe", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 28

Answer: assert Solution().shiftDistance(s = "abcdefgh", t = "hgfedcba", nextCost = [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52], previousCost = [52,50,48,46,44,42,40,38,36,34,32,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2]) == 832

Answer: assert Solution().shiftDistance(s = "abcde", t = "edcba", nextCost = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26], previousCost = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) == 156

Answer: assert Solution().shiftDistance(s = "xyzxyz", t = "wvwvwv", nextCost = [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], previousCost = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]) == 350

Answer: assert Solution().shiftDistance(s = "abacabadabacaba", t = "bcbbacebbcaecbb", nextCost = [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5], previousCost = [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]) == 90

Answer: assert Solution().shiftDistance(s = "python", t = "rubyon", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 23

Answer: assert Solution().shiftDistance(s = "mnopqrstuvwxyzabcdefghijkl", t = "qrstuvwxyzabcdefghijklmno", nextCost = [5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,1,2,3,4], previousCost = [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,1,2,3]) == 1334

Answer: assert Solution().shiftDistance(s = "abcabcabcabc", t = "bcabcbabcbca", nextCost = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,1], previousCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]) == 28

Answer: assert Solution().shiftDistance(s = "aaaazzzz", t = "zzzzaaaa", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 8

Answer: assert Solution().shiftDistance(s = "qwert", t = "rtyui", nextCost = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260], previousCost = [260, 250, 240, 230, 220, 210, 200, 190, 180, 170, 160, 150, 140, 130, 120, 110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10]) == 3420

Answer: assert Solution().shiftDistance(s = "a", t = "z", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 26

Answer: assert Solution().shiftDistance(s = "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc", t = "xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 2808

Answer: assert Solution().shiftDistance(s = "abcdefghij", t = "jihgfedcba", nextCost = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260], previousCost = [260,250,240,230,220,210,200,190,180,170,160,150,140,130,120,110,100,90,80,70,60,50,40,30,20,10]) == 6500

Answer: assert Solution().shiftDistance(s = "abcxyz", t = "xyzabc", nextCost = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260], previousCost = [260,250,240,230,220,210,200,190,180,170,160,150,140,130,120,110,100,90,80,70,60,50,40,30,20,10]) == 3120

Answer: assert Solution().shiftDistance(s = "aaaaaaaa", t = "zzzzzzzz", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 208

Answer: assert Solution().shiftDistance(s = "abcdeabcde", t = "edcbaedcba", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 312

Answer: assert Solution().shiftDistance(s = "zyxzyxzyxzyx", t = "abcabcabcabc", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 36

Answer: assert Solution().shiftDistance(s = "qwertyuiopasdfghjklzxcvbnm", t = "mnbvcxzlkjhgfdsapoiuytrewq", nextCost = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260], previousCost = [260,250,240,230,220,210,200,190,180,170,160,150,140,130,120,110,100,90,80,70,60,50,40,30,20,10]) == 18200

Answer: assert Solution().shiftDistance(s = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", t = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 50

Answer: assert Solution().shiftDistance(s = "abcdefghijklmnopqrstuvwxyz", t = "abcdefghijklmnopqrstuvwxyz", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 0

Answer: assert Solution().shiftDistance(s = "aaaabbbb", t = "zzzzxxxx", nextCost = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26], previousCost = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) == 320

Answer: assert Solution().shiftDistance(s = "abcd", t = "dcba", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 104

Answer: assert Solution().shiftDistance(s = "python", t = "typhon", nextCost = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26], previousCost = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) == 104

Answer: assert Solution().shiftDistance(s = "abcdef", t = "ghijkl", nextCost = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130], previousCost = [130, 125, 120, 115, 110, 105, 100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5]) == 1080

Answer: assert Solution().shiftDistance(s = "aaaaabbbbb", t = "zzzzzzyyyy", nextCost = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26], previousCost = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) == 389

Answer: assert Solution().shiftDistance(s = "zzzzzzzz", t = "aaaaaaaa", nextCost = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26], previousCost = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) == 208

Answer: assert Solution().shiftDistance(s = "programming", t = "codingzoo", nextCost = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260], previousCost = [260,250,240,230,220,210,200,190,180,170,160,150,140,130,120,110,100,90,80,70,60,50,40,30,20,10]) == 8590

Answer: assert Solution().shiftDistance(s = "abababab", t = "babababa", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 8

Answer: assert Solution().shiftDistance(s = "abcdefghijklmnopqrstuvwxyz", t = "bcdefghijklmnopqrstuvwxyza", nextCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1], previousCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]) == 351

Answer: assert Solution().shiftDistance(s = "abcdef", t = "fedcba", nextCost = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], previousCost = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]) == 252

Answer: assert Solution().shiftDistance(s = "hello", t = "world", nextCost = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], previousCost = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) == 38

Answer: assert Solution().shiftDistance(s = "xyzabc", t = "wvutsr", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 36

Answer: assert Solution().shiftDistance(s = "abcdefghijklmnopqrstuvwxyz", t = "abcdefghijklmnopqrstuvwxyz", nextCost = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26], previousCost = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) == 0

Answer: assert Solution().shiftDistance(s = "abcdefg", t = "gfedcba", nextCost = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260], previousCost = [260,250,240,230,220,210,200,190,180,170,160,150,140,130,120,110,100,90,80,70,60,50,40,30,20,10]) == 3120

Answer: assert Solution().shiftDistance(s = "mnopqr", t = "qrstuv", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 408

Answer: assert Solution().shiftDistance(s = "abcdxyz", t = "zyxabc", nextCost = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260], previousCost = [260, 250, 240, 230, 220, 210, 200, 190, 180, 170, 160, 150, 140, 130, 120, 110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10]) == 3580

Answer: assert Solution().shiftDistance(s = "bbbbbbbbbbbb", t = "aaaaaaaaaaaa", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 300

Answer: assert Solution().shiftDistance(s = "alibaba", t = "babibib", nextCost = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260], previousCost = [260,250,240,230,220,210,200,190,180,170,160,150,140,130,120,110,100,90,80,70,60,50,40,30,20,10]) == 4400

Answer: assert Solution().shiftDistance(s = "aaaabbbb", t = "zzzzzzzz", nextCost = [1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000], previousCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]) == 16

Answer: assert Solution().shiftDistance(s = "abcd", t = "dcba", nextCost = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130], previousCost = [130, 125, 120, 115, 110, 105, 100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5]) == 520

Answer: assert Solution().shiftDistance(s = "aaaaaabbbbb", t = "bbbbbbaaaaa", nextCost = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]) == 0

Answer: assert Solution().shiftDistance(s = "quickbrownfox", t = "vbnhwnljwyd", nextCost = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], previousCost = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) == 122

Answer: assert Solution().shiftDistance(s = "aabbaa", t = "zzxxzz", nextCost = [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0], previousCost = [0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]) == 4

Answer: assert Solution().shiftDistance(s = "pqrstuvw", t = "vwxyzabc", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [9,8,7,6,5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 48

Answer: assert Solution().shiftDistance(s = "abcxyz", t = "zxycba", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 312

Answer: assert Solution().shiftDistance(s = "hello", t = "world", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 555

Answer: assert Solution().shiftDistance(s = "aaabbb", t = "bbbccc", nextCost = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) == 0

Answer: assert Solution().shiftDistance(s = "qwerasdfzxcv", t = "lkjhgfdsvbnm", nextCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1], previousCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]) == 1500

Answer: assert Solution().shiftDistance(s = "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz", t = "zyxwvutsrqponmlkjihgfedcbazyxwvutsrqponmlkjihgfedcba", nextCost = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260], previousCost = [260,250,240,230,220,210,200,190,180,170,160,150,140,130,120,110,100,90,80,70,60,50,40,30,20,10]) == 46020

Answer: assert Solution().shiftDistance(s = "bobby", t = "yyllo", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 442

Answer: assert Solution().shiftDistance(s = "aabbccddeeff", t = "zzxxwwvvttrr", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 1096

Answer: assert Solution().shiftDistance(s = "abcdefghijklmnopqrstuvwxyz", t = "abcdefghijklmnopqrstuvwxyza", nextCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0], previousCost = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]) == 0

Answer: assert Solution().shiftDistance(s = "hello", t = "world", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]) == 38

Answer: assert Solution().shiftDistance(s = "aabbcc", t = "bbccdd", nextCost = [5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130], previousCost = [130,125,120,115,110,105,100,95,90,85,80,75,70,65,60,55,50,45,40,35,30,25,20,15,10,5]) == 60

Answer: assert Solution().shiftDistance(s = "aaaaaaaaaa", t = "zzzzzzzzzz", nextCost = [5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5]) == 0

Answer: assert Solution().shiftDistance(s = "xyzabc", t = "abcdef", nextCost = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260], previousCost = [260,250,240,230,220,210,200,190,180,170,160,150,140,130,120,110,100,90,80,70,60,50,40,30,20,10]) == 1830

Answer: assert Solution().shiftDistance(s = "zyxwvutsrqponmlkjihgfedcba", t = "abcdefghijklmnopqrstuvwxyz", nextCost = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26], previousCost = [26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]) == 2366

import random
import functools
import collections
import string
import math
import datetime

from typing import *
from functools import *
from collections import *
from itertools import *
from heapq import *
from bisect import *
from string import *
from operator import *
from math import *

inf = float('inf')

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def list_node(values: list):
    if not values:
        return None
    head = ListNode(values[0])
    p = head
    for val in values[1:]:
        node = ListNode(val)
        p.next = node
        p = node
    return head

def is_same_list(p1, p2):
    if p1 is None and p2 is None:
        return True
    if not p1 or not p2:
        return False
    return p1.val == p2.val and is_same_list(p1.next, p2.next)

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def tree_node(values: list):
    if not values:
        return None
    root = TreeNode(values[0])
    i = 1
    queue = deque()
    queue.append(root)
    while queue:
        node = queue.popleft()
        if i < len(values) and values[i] is not None:
            node.left = TreeNode(values[i])
            queue.append(node.left)
        i += 1
        if i < len(values) and values[i] is not None:
            node.right = TreeNode(values[i])
            queue.append(node.right)
        i += 1
    return root

def is_same_tree(p, q):
    if not p and not q:
        return True
    elif not p or not q:
        return False
    elif p.val != q.val:
        return False
    else:
        return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)

class Solution:
    def shiftDistance(
        self, s: str, t: str, nextCost: List[int], previousCost: List[int]
    ) -> int:
        m = 26
        s1 = [0] * (m << 1 | 1)
        s2 = [0] * (m << 1 | 1)
        for i in range(m << 1):
            s1[i + 1] = s1[i] + nextCost[i % m]
            s2[i + 1] = s2[i] + previousCost[(i + 1) % m]
        ans = 0
        for a, b in zip(s, t):
            x, y = ord(a) - ord("a"), ord(b) - ord("a")
            c1 = s1[y + m if y < x else y] - s1[x]
            c2 = s2[x + m if x < y else x] - s2[y]
            ans += min(c1, c2)
        return ans


You are an expert Python programmer. Write correct, efficient Python 3 code that solves the problem and passes all tests. Output the code in the following format: ```python
<code>
```

PROBLEM:
There is a game dungeon comprised of n x n rooms arranged in a grid.
You are given a 2D array fruits of size n x n, where fruits[i][j] represents the number of fruits in the room (i, j). Three children will play in the game dungeon, with initial positions at the corner rooms (0, 0), (0, n - 1), and (n - 1, 0).
The children will make exactly n - 1 moves according to the following rules to reach the room (n - 1, n - 1):

The child starting from (0, 0) must move from their current room (i, j) to one of the rooms (i + 1, j + 1), (i + 1, j), and (i, j + 1) if the target room exists.
The child starting from (0, n - 1) must move from their current room (i, j) to one of the rooms (i + 1, j - 1), (i + 1, j), and (i + 1, j + 1) if the target room exists.
The child starting from (n - 1, 0) must move from their current room (i, j) to one of the rooms (i - 1, j + 1), (i, j + 1), and (i + 1, j + 1) if the target room exists.

When a child enters a room, they will collect all the fruits there. If two or more children enter the same room, only one child will collect the fruits, and the room will be emptied after they leave.
Return the maximum number of fruits the children can collect from the dungeon.

Example 1:

Input: fruits = [[1,2,3,4],[5,6,8,7],[9,10,11,12],[13,14,15,16]]
Output: 100
Explanation:

In this example:

The 1st child (green) moves on the path (0,0) -> (1,1) -> (2,2) -> (3, 3).
The 2nd child (red) moves on the path (0,3) -> (1,2) -> (2,3) -> (3, 3).
The 3rd child (blue) moves on the path (3,0) -> (3,1) -> (3,2) -> (3, 3).

In total they collect 1 + 6 + 11 + 16 + 4 + 8 + 12 + 13 + 14 + 15 = 100 fruits.

Example 2:

Input: fruits = [[1,1],[1,1]]
Output: 4
Explanation:
In this example:

The 1st child moves on the path (0,0) -> (1,1).
The 2nd child moves on the path (0,1) -> (1,1).
The 3rd child moves on the path (1,0) -> (1,1).

In total they collect 1 + 1 + 1 + 1 = 4 fruits.

Constraints:

2 <= n == fruits.length == fruits[i].length <= 1000
0 <= fruits[i][j] <= 1000

Your solution to the problem should be a method of the class Solution called maxCollectedFruits and should pass all tests. Use the following starter code:
```python
class Solution:
    def maxCollectedFruits(self, fruits: List[List[int]]) -> int:
        # Add your code here
```

SOLUTION:

Question:
PROBLEM:
There is a game dungeon comprised of n x n rooms arranged in a grid.
You are given a 2D array fruits of size n x n, where fruits[i][j] represents the number of fruits in the room (i, j). Three children will play in the game dungeon, with initial positions at the corner rooms (0, 0), (0, n - 1), and (n - 1, 0).
The children will make exactly n - 1 moves according to the following rules to reach the room (n - 1, n - 1):

The child starting from (0, 0) must move from their current room (i, j) to one of the rooms (i + 1, j + 1), (i + 1, j), and (i, j + 1) if the target room exists.
The child starting from (0, n - 1) must move from their current room (i, j) to one of the rooms (i + 1, j - 1), (i + 1, j), and (i + 1, j + 1) if the target room exists.
The child starting from (n - 1, 0) must move from their current room (i, j) to one of the rooms (i - 1, j + 1), (i, j + 1), and (i + 1, j + 1) if the target room exists.

When a child enters a room, they will collect all the fruits there. If two or more children enter the same room, only one child will collect the fruits, and the room will be emptied after they leave.
Return the maximum number of fruits the children can collect from the dungeon.

Example 1:

Input: fruits = [[1,2,3,4],[5,6,8,7],[9,10,11,12],[13,14,15,16]]
Output: 100
Explanation:

In this example:

The 1st child (green) moves on the path (0,0) -> (1,1) -> (2,2) -> (3, 3).
The 2nd child (red) moves on the path (0,3) -> (1,2) -> (2,3) -> (3, 3).
The 3rd child (blue) moves on the path (3,0) -> (3,1) -> (3,2) -> (3, 3).

In total they collect 1 + 6 + 11 + 16 + 4 + 8 + 12 + 13 + 14 + 15 = 100 fruits.

Example 2:

Input: fruits = [[1,1],[1,1]]
Output: 4
Explanation:
In this example:

The 1st child moves on the path (0,0) -> (1,1).
The 2nd child moves on the path (0,1) -> (1,1).
The 3rd child moves on the path (1,0) -> (1,1).

In total they collect 1 + 1 + 1 + 1 = 4 fruits.

Constraints:

2 <= n == fruits.length == fruits[i].length <= 1000
0 <= fruits[i][j] <= 1000

Your solution to the problem should be a method of the class Solution called maxCollectedFruits and should pass all tests. Use the following starter code:
```python
class Solution:
    def maxCollectedFruits(self, fruits: List[List[int]]) -> int:
        # Add your code here
```

SOLUTION:

assert Solution().maxCollectedFruits(fruits = [[1,1],[1,1]]) == 4

assert Solution().maxCollectedFruits(fruits = [[1,2],[3,4]]) == 10

assert Solution().maxCollectedFruits(fruits = [[0,0,0],[0,0,0],[0,0,0]]) == 0

assert Solution().maxCollectedFruits(fruits = [[1,2,3],[4,5,6],[7,8,9]]) == 39

assert Solution().maxCollectedFruits(fruits = [[5,5,5],[5,5,5],[5,5,5]]) == 35

assert Solution().maxCollectedFruits(fruits = [[1000,1000],[1000,1000]]) == 4000

assert Solution().maxCollectedFruits(fruits = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) == 39

Answer: assert Solution().maxCollectedFruits(fruits = [[10,20,30,40],[50,60,70,80],[90,100,110,120],[130,140,150,160]]) == 1000

Answer: assert Solution().maxCollectedFruits(fruits = [[1000,0,0],[0,1000,0],[0,0,1000]]) == 3000

Answer: assert Solution().maxCollectedFruits(fruits = [[10,0,0,10],[0,1,1,0],[0,1,1,0],[10,0,0,10]]) == 44

Answer: assert Solution().maxCollectedFruits(fruits = [[1,1],[1,1]]) == 4

Answer: assert Solution().maxCollectedFruits(fruits = [[1,2],[3,4]]) == 10

Answer: assert Solution().maxCollectedFruits(fruits = [[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5],[5,5,5,5,5]]) == 65

Answer: assert Solution().maxCollectedFruits(fruits = [[0,0,0],[0,0,0],[0,0,0]]) == 0

Answer: assert Solution().maxCollectedFruits(fruits = [[1,2,3,4],[5,6,8,7],[9,10,11,12],[13,14,15,16]]) == 100

Answer: assert Solution().maxCollectedFruits(fruits = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]) == 28

Answer: assert Solution().maxCollectedFruits(fruits = [[1,2,3],[4,5,6],[7,8,9]]) == 39

Answer: assert Solution().maxCollectedFruits(fruits = [[5,5,5],[5,5,5],[5,5,5]]) == 35

Answer: assert Solution().maxCollectedFruits(fruits = [[10,20,30],[40,50,60],[70,80,90]]) == 390

Answer: assert Solution().maxCollectedFruits(fruits = [[1000,1000],[1000,1000]]) == 4000

Answer: assert Solution().maxCollectedFruits(fruits = [[5,10,15,20,25],[10,15,20,25,30],[15,20,25,30,35],[20,25,30,35,40],[25,30,35,40,45]]) == 385

Answer: assert Solution().maxCollectedFruits(fruits = [[5, 10, 15, 20, 25], [30, 35, 40, 45, 50], [55, 60, 65, 70, 75], [80, 85, 90, 95, 100], [105, 110, 115, 120, 125]]) == 1025

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8], [4, 5, 6, 7, 8, 9], [5, 6, 7, 8, 9, 10], [6, 7, 8, 9, 10, 11]]) == 116

Answer: assert Solution().maxCollectedFruits(fruits = [[1,2,3,4,5,6,7],[8,9,10,11,12,13,14],[15,16,17,18,19,20,21],[22,23,24,25,26,27,28],[29,30,31,32,33,34,35],[36,37,38,39,40,41,42],[43,44,45,46,47,48,49]]) == 595

Answer: assert Solution().maxCollectedFruits(fruits = [[1,0,0,0,1],[0,1,0,1,0],[0,0,1,0,0],[0,1,0,1,0],[1,0,0,0,1]]) == 9

Answer: assert Solution().maxCollectedFruits(fruits = [[0, 0, 0, 0, 1000], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1000, 0, 0, 0, 0]]) == 2000

Answer: assert Solution().maxCollectedFruits(fruits = [[0,0,100,0,0],[0,100,0,100,0],[100,0,100,0,100],[0,100,0,100,0],[0,0,100,0,0]]) == 700

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 3, 1, 5, 2], [2, 8, 4, 6, 3], [5, 7, 10, 9, 2], [3, 6, 5, 8, 4], [2, 9, 4, 3, 7]]) == 74

Answer: assert Solution().maxCollectedFruits(fruits = [[0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5], [0, 2, 3, 4, 5, 6], [0, 3, 4, 5, 6, 7], [0, 4, 5, 6, 7, 8], [0, 5, 6, 7, 8, 9]]) == 77

Answer: assert Solution().maxCollectedFruits(fruits = [[0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 0], [0, 0, 1, 0, 1, 0, 0], [0, 1, 0, 1, 0, 1, 0], [0, 0, 1, 0, 1, 0, 0], [0, 1, 0, 1, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1]]) == 12

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 10], [5, 6, 7, 8, 9, 10, 11], [6, 7, 8, 9, 10, 11, 12], [7, 8, 9, 10, 11, 12, 13]]) == 163

Answer: assert Solution().maxCollectedFruits(fruits = [[0,1,0,1,0],[1,0,1,0,1],[0,1,0,1,0],[1,0,1,0,1],[0,1,0,1,0]]) == 6

Answer: assert Solution().maxCollectedFruits(fruits = [[0,100,200,300,400,500],[500,400,300,200,100,0],[0,1,2,3,4,5],[5,4,3,2,1,0],[100,200,300,400,500,600],[600,500,400,300,200,100]]) == 4310

Answer: assert Solution().maxCollectedFruits(fruits = [[1,3,5,7,9],[2,4,6,8,10],[11,13,15,17,19],[12,14,16,18,20],[21,23,25,27,29]]) == 221

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36]]) == 366

Answer: assert Solution().maxCollectedFruits(fruits = [[1000,0,0,0,0],[0,1000,0,0,0],[0,0,1000,0,0],[0,0,0,1000,0],[0,0,0,0,1000]]) == 5000

Answer: assert Solution().maxCollectedFruits(fruits = [[100,0,0,0,100],[0,50,50,50,0],[0,50,100,50,0],[0,50,50,50,0],[100,0,0,0,100]]) == 800

Answer: assert Solution().maxCollectedFruits(fruits = [[1,0,0,0,0,0,1],[0,1,0,0,0,1,0],[0,0,1,0,1,0,0],[0,0,0,10,0,0,0],[0,0,1,0,1,0,0],[0,1,0,0,0,1,0],[1,0,0,0,0,0,1]]) == 22

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 2, 3, 4], [8, 7, 6, 5], [12, 13, 14, 15], [9, 10, 11, 16]]) == 96

Answer: assert Solution().maxCollectedFruits(fruits = [[1,0,0,0,0,0,0,0,0,1],[0,1,0,0,0,0,0,0,1,0],[0,0,1,0,0,0,0,1,0,0],[0,0,0,1,0,0,1,0,0,0],[0,0,0,0,1,1,0,0,0,0],[0,0,0,1,1,0,0,0,0,0],[0,0,1,0,0,0,0,0,0,0],[0,1,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0]]) == 15

Answer: assert Solution().maxCollectedFruits(fruits = [[0,0,0,0,1000],[0,0,0,1000,0],[0,0,1000,0,0],[0,1000,0,0,0],[1000,0,0,0,0]]) == 5000

Answer: assert Solution().maxCollectedFruits(fruits = [[1000,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,1000]]) == 2000

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 100, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]) == 112

Answer: assert Solution().maxCollectedFruits(fruits = [[100,200,300],[400,500,600],[700,800,900]]) == 3900

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 2, 3], [4, 5, 6], [7, 8, 1000]]) == 1030

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0], [1, 1, 1, 1]]) == 7

Answer: assert Solution().maxCollectedFruits(fruits = [[100,101,102,103,104],[105,106,107,108,109],[110,111,112,113,114],[115,116,117,118,119],[120,121,122,123,124]]) == 1492

Answer: assert Solution().maxCollectedFruits(fruits = [[1000, 0, 0, 0, 1000], [0, 1000, 0, 1000, 0], [0, 0, 1000, 0, 0], [0, 1000, 0, 1000, 0], [1000, 0, 0, 0, 1000]]) == 9000

Answer: assert Solution().maxCollectedFruits(fruits = [[100,150,200,250],[300,350,400,450],[500,550,600,650],[700,750,800,850]]) == 5500

Answer: assert Solution().maxCollectedFruits(fruits = [[5,5,5,5],[5,1,1,5],[5,1,1,5],[5,5,5,5]]) == 42

Answer: assert Solution().maxCollectedFruits(fruits = [[10,10,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,10]]) == 20

Answer: assert Solution().maxCollectedFruits(fruits = [[100, 50, 200, 100, 300], [200, 100, 300, 400, 500], [300, 200, 100, 500, 600], [400, 300, 200, 100, 700], [500, 400, 300, 200, 800]]) == 4700

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 0, 3, 2, 4, 1], [1, 2, 0, 3, 1, 4], [3, 1, 2, 0, 4, 1], [2, 4, 1, 3, 0, 2], [4, 1, 3, 2, 1, 0], [0, 4, 1, 2, 3, 1]]) == 33

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 0, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]) == 205

Answer: assert Solution().maxCollectedFruits(fruits = [[0, 1000, 0], [1000, 0, 1000], [0, 1000, 0]]) == 2000

Answer: assert Solution().maxCollectedFruits(fruits = [[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120], [130, 140, 150, 160]]) == 1000

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4], [5, 5, 5, 5, 5]]) == 45

Answer: assert Solution().maxCollectedFruits(fruits = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]) == 4

Answer: assert Solution().maxCollectedFruits(fruits = [[1,1,1,1],[1,10,10,1],[1,10,10,1],[1,1,1,1]]) == 46

Answer: assert Solution().maxCollectedFruits(fruits = [[1,2,3,4,5],[5,4,3,2,1],[1,3,5,7,9],[9,7,5,3,1],[1,2,3,4,5]]) == 52

Answer: assert Solution().maxCollectedFruits(fruits = [[100,0,0,0,0],[0,100,0,0,0],[0,0,100,0,0],[0,0,0,100,0],[0,0,0,0,100]]) == 500

Answer: assert Solution().maxCollectedFruits(fruits = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,25]]) == 205

Answer: assert Solution().maxCollectedFruits(fruits = [[50,0,50,0,50,0],[0,100,0,100,0,100],[50,0,50,0,50,0],[0,100,0,100,0,100],[50,0,50,0,50,0],[0,100,0,100,0,100]]) == 950

Answer: assert Solution().maxCollectedFruits(fruits = [[500,501,502,503],[504,505,506,507],[508,509,510,511],[512,513,514,515]]) == 5090

Answer: assert Solution().maxCollectedFruits(fruits = [[100, 200, 300, 400], [500, 600, 700, 800], [900, 1000, 1100, 1200], [1300, 1400, 1500, 1600]]) == 10000

Answer: assert Solution().maxCollectedFruits(fruits = [[1,5,3,7,9],[2,6,8,4,10],[11,13,12,14,15],[16,18,17,19,20],[21,23,22,24,25]]) == 207

Answer: assert Solution().maxCollectedFruits(fruits = [[0, 0, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 1]]) == 6

Answer: assert Solution().maxCollectedFruits(fruits = [[0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7], [3, 4, 5, 6, 7, 8], [4, 5, 6, 7, 8, 9], [5, 6, 7, 8, 9, 10]]) == 100

Answer: assert Solution().maxCollectedFruits(fruits = [[100,100,100,100],[100,100,100,100],[100,100,100,100],[100,100,100,100]]) == 1000

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1000]]) == 1009

Answer: assert Solution().maxCollectedFruits(fruits = [[100, 200, 300, 400, 500], [500, 400, 300, 200, 100], [100, 200, 300, 400, 500], [500, 400, 300, 200, 100], [100, 200, 300, 400, 500]]) == 4000

Answer: assert Solution().maxCollectedFruits(fruits = [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,24],[25,26,27,28,29,30],[31,32,33,34,35,36]]) == 366

Answer: assert Solution().maxCollectedFruits(fruits = [[0, 1, 2, 3, 4, 5, 6, 7], [7, 6, 5, 4, 3, 2, 1, 0], [0, 1, 2, 3, 4, 5, 6, 7], [7, 6, 5, 4, 3, 2, 1, 0], [0, 1, 2, 3, 4, 5, 6, 7], [7, 6, 5, 4, 3, 2, 1, 0], [0, 1, 2, 3, 4, 5, 6, 7], [7, 6, 5, 4, 3, 2, 1, 0]]) == 87

Answer: assert Solution().maxCollectedFruits(fruits = [[1,3,1,5,2],[2,1,4,1,3],[5,2,3,4,1],[1,3,5,2,4],[2,4,1,3,5]]) == 39

Answer: assert Solution().maxCollectedFruits(fruits = [[100,200,300,400,500],[500,100,200,300,400],[400,500,100,200,300],[300,400,500,100,200],[200,300,400,500,100]]) == 3500

Answer: assert Solution().maxCollectedFruits(fruits = [[90, 80, 70, 60], [50, 40, 30, 20], [10, 20, 30, 40], [50, 60, 70, 80]]) == 550

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1], [1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1]]) == 12

Answer: assert Solution().maxCollectedFruits(fruits = [[999,999,999,999],[999,999,999,999],[999,999,999,999],[999,999,999,999]]) == 9990

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) == 39

Answer: assert Solution().maxCollectedFruits(fruits = [[100, 50, 30, 20, 10], [10, 20, 30, 40, 50], [50, 40, 30, 20, 10], [10, 20, 30, 40, 50], [50, 10, 20, 30, 40]]) == 490

Answer: assert Solution().maxCollectedFruits(fruits = [[10, 20, 30, 40, 50], [40, 30, 20, 10, 50], [50, 10, 40, 20, 30], [20, 50, 10, 40, 30], [30, 20, 50, 10, 40]]) == 460

Answer: assert Solution().maxCollectedFruits(fruits = [[5, 5, 5, 5, 5], [5, 5, 5, 5, 5], [5, 5, 5, 5, 5], [5, 5, 5, 5, 5], [5, 5, 5, 5, 5]]) == 65

Answer: assert Solution().maxCollectedFruits(fruits = [[9,0,0,0,0],[0,8,0,0,0],[0,0,7,0,0],[0,0,0,6,0],[0,0,0,0,5]]) == 35

Answer: assert Solution().maxCollectedFruits(fruits = [[5,1,1,1,5],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[5,1,1,1,5]]) == 29

Answer: assert Solution().maxCollectedFruits(fruits = [[10, 20, 30, 40, 50, 60], [20, 30, 40, 50, 60, 70], [30, 40, 50, 60, 70, 80], [40, 50, 60, 70, 80, 90], [50, 60, 70, 80, 90, 100], [60, 70, 80, 90, 100, 110]]) == 1160

Answer: assert Solution().maxCollectedFruits(fruits = [[1,0,1,0,1,0,1],[0,1,0,1,0,1,0],[1,0,1,0,1,0,1],[0,1,0,1,0,1,0],[1,0,1,0,1,0,1],[0,1,0,1,0,1,0],[1,0,1,0,1,0,1]]) == 17

Answer: assert Solution().maxCollectedFruits(fruits = [[10,100,10,100],[100,10,100,10],[10,100,10,100],[100,10,100,10]]) == 640

Answer: assert Solution().maxCollectedFruits(fruits = [[1000,0,0,0,0],[0,900,0,0,0],[0,0,800,0,0],[0,0,0,700,0],[0,0,0,0,600]]) == 4000

Answer: assert Solution().maxCollectedFruits(fruits = [[10,20,30,40,50],[50,40,30,20,10],[10,20,30,40,50],[50,40,30,20,10],[10,20,30,40,50]]) == 400

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1], [1, 1, 100, 1, 1, 100, 1], [1, 1, 1, 100, 1, 1, 1], [1, 1, 100, 1, 100, 1, 1], [1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1]]) == 514

Answer: assert Solution().maxCollectedFruits(fruits = [[0,0,0,100],[0,0,100,0],[0,100,0,0],[100,0,0,0]]) == 400

Answer: assert Solution().maxCollectedFruits(fruits = [[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7],[4,5,6,7,8],[5,6,7,8,9]]) == 77

Answer: assert Solution().maxCollectedFruits(fruits = [[1, 0, 0, 1, 0], [0, 1, 0, 0, 1], [0, 0, 1, 0, 0], [1, 0, 0, 1, 0], [0, 1, 0, 0, 1]]) == 7

Answer: assert Solution().maxCollectedFruits(fruits = [[0, 0, 0, 0, 0], [0, 0, 1000, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]) == 0

import random
import functools
import collections
import string
import math
import datetime

from typing import *
from functools import *
from collections import *
from itertools import *
from heapq import *
from bisect import *
from string import *
from operator import *
from math import *

inf = float('inf')

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def list_node(values: list):
    if not values:
        return None
    head = ListNode(values[0])
    p = head
    for val in values[1:]:
        node = ListNode(val)
        p.next = node
        p = node
    return head

def is_same_list(p1, p2):
    if p1 is None and p2 is None:
        return True
    if not p1 or not p2:
        return False
    return p1.val == p2.val and is_same_list(p1.next, p2.next)

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def tree_node(values: list):
    if not values:
        return None
    root = TreeNode(values[0])
    i = 1
    queue = deque()
    queue.append(root)
    while queue:
        node = queue.popleft()
        if i < len(values) and values[i] is not None:
            node.left = TreeNode(values[i])
            queue.append(node.left)
        i += 1
        if i < len(values) and values[i] is not None:
            node.right = TreeNode(values[i])
            queue.append(node.right)
        i += 1
    return root

def is_same_tree(p, q):
    if not p and not q:
        return True
    elif not p or not q:
        return False
    elif p.val != q.val:
        return False
    else:
        return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)

class Solution:
  def maxCollectedFruits(self, fruits: list[list[int]]) -> int:
    n = len(fruits)

    def getTopLeft() -> int:
      return sum(fruits[i][i] for i in range(n))

    def getTopRight() -> int:
      # dp[i][j] := the number of fruits collected from (0, n - 1) to (i, j)
      dp = [[0] * n for _ in range(n)]
      dp[0][-1] = fruits[0][-1]
      for x in range(n):
        for y in range(n):
          if x >= y and (x, y) != (n - 1, n - 1):
            continue
          for dx, dy in [(1, -1), (1, 0), (1, 1)]:
            i = x - dx
            j = y - dy
            if i < 0 or i == n or j < 0 or j == n:
              continue
            if i < j < n - 1 - i:
              continue
            dp[x][y] = max(dp[x][y], dp[i][j] + fruits[x][y])
      return dp[-1][-1]

    def getBottomLeft() -> int:
      # dp[i][j] := the number of fruits collected from (n - 1, 0) to (i, j)
      dp = [[0] * n for _ in range(n)]
      dp[-1][0] = fruits[-1][0]
      for y in range(n):
        for x in range(n):
          if x <= y and (x, y) != (n - 1, n - 1):
            continue
          for dx, dy in [(-1, 1), (0, 1), (1, 1)]:
            i = x - dx
            j = y - dy
            if i < 0 or i == n or j < 0 or j == n:
              continue
            if j < i < n - 1 - j:
              continue
            dp[x][y] = max(dp[x][y], dp[i][j] + fruits[x][y])
      return dp[-1][-1]

    return getTopLeft() + getTopRight() + getBottomLeft() - 2 * fruits[-1][-1]