comefrom: <doocs>1463.Cherry Pickup II_1.py
code: def cherryPickup( grid: List[List[int]]) -> int:
      m, n = len(grid), len(grid[0])
      dp = [[[0] * n for _ in range(n)] for _ in range(m)]
      valid = [[[False] * n for _ in range(n)] for _ in range(m)]
      dp[0][0][n - 1] = grid[0][0] + grid[0][n - 1]
      valid[0][0][n - 1] = True
      for i in range(1, m):
          for j1 in range(n):
              for j2 in range(n):
                  t = grid[i][j1]
                  if j1 != j2:
                      t += grid[i][j2]
                  ok = False
                  for y1 in range(j1 - 1, j1 + 2):
                      for y2 in range(j2 - 1, j2 + 2):
                          if 0 <= y1 < n and 0 <= y2 < n and valid[i - 1][y1][y2]:
                              dp[i][j1][j2] = max(
                                  dp[i][j1][j2], dp[i - 1][y1][y2] + t
                              )
                              ok = True
                  valid[i][j1][j2] = ok
      return max(dp[m - 1][j1][j2] for j1 in range(n) for j2 in range(n))
masked: def sample_funcname ( var_0 : List [ List [ int ] ] ) -> int :
      var_1 , var_2 = len ( var_0 ) , len ( var_0 [ 0 ] )
      var_4 = [ [ [ 0 ] * var_2 for var_3 in range ( var_2 ) ] for var_3 in range ( var_1 ) ]
      var_5 = [ [ [ False ] * var_2 for var_3 in range ( var_2 ) ] for var_3 in range ( var_1 ) ]
      var_4 [ 0 ] [ 0 ] [ var_2 - 1 ] = var_0 [ 0 ] [ 0 ] + var_0 [ 0 ] [ var_2 - 1 ]
      var_5 [ 0 ] [ 0 ] [ var_2 - 1 ] = True
      for var_6 in range ( 1 , var_1 ) :
          for var_7 in range ( var_2 ) :
              for var_8 in range ( var_2 ) :
                  var_9 = var_0 [ var_6 ] [ var_7 ]
                  if var_7 != var_8 :
                      var_9 += var_0 [ var_6 ] [ var_8 ]
                  var_10 = False
                  for var_11 in range ( var_7 - 1 , var_7 + 2 ) :
                      for var_12 in range ( var_8 - 1 , var_8 + 2 ) :
                          if 0 <= var_11 < var_2 and 0 <= var_12 < var_2 and var_5 [ var_6 - 1 ] [ var_11 ] [ var_12 ] :
                              var_4 [ var_6 ] [ var_7 ] [ var_8 ] = max ( var_4 [ var_6 ] [ var_7 ] [ var_8 ] , var_4 [ var_6 - 1 ] [ var_11 ] [ var_12 ] + var_9 )
                              var_10 = True
                  var_5 [ var_6 ] [ var_7 ] [ var_8 ] = var_10
      return max ( var_4 [ var_1 - 1 ] [ var_7 ] [ var_8 ] for var_7 in range ( var_2 ) for var_8 in range ( var_2 ) )
  