<doocs>1463.Cherry Pickup II_1.py
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))
def sample_funcname ( var_0 : List [ List [ int ] ] ) -> int :
    var_4 , var_6 = len ( var_0 ) , len ( var_0 [ 0 ] )
    var_9 = [ [ [ 0 ] * var_6 for var_7 in range ( var_6 ) ] for var_7 in range ( var_4 ) ]
    var_11 = [ [ [ False ] * var_6 for var_7 in range ( var_6 ) ] for var_7 in range ( var_4 ) ]
    var_9 [ 0 ] [ 0 ] [ var_6 - 1 ] = var_0 [ 0 ] [ 0 ] + var_0 [ 0 ] [ var_6 - 1 ]
    var_11 [ 0 ] [ 0 ] [ var_6 - 1 ] = True
    for var_14 in range ( 1 , var_4 ) :
        for var_15 in range ( var_6 ) :
            for var_16 in range ( var_6 ) :
                var_17 = var_0 [ var_14 ] [ var_15 ]
                if var_15 != var_16 :
                    var_17 += var_0 [ var_14 ] [ var_16 ]
                var_18 = False
                for var_20 in range ( var_15 - 1 , var_15 + 2 ) :
                    for var_21 in range ( var_16 - 1 , var_16 + 2 ) :
                        if 0 <= var_20 < var_6 and 0 <= var_21 < var_6 and var_11 [ var_14 - 1 ] [ var_20 ] [ var_21 ] :
                            var_9 [ var_14 ] [ var_15 ] [ var_16 ] = max ( var_9 [ var_14 ] [ var_15 ] [ var_16 ] , var_9 [ var_14 - 1 ] [ var_20 ] [ var_21 ] + var_17 )
                            var_18 = True
                var_11 [ var_14 ] [ var_15 ] [ var_16 ] = var_18
    return max ( var_9 [ var_4 - 1 ] [ var_15 ] [ var_16 ] for var_15 in range ( var_6 ) for var_16 in range ( var_6 ) )
