If already calculated
if dp [ mask ] [ prev ] [ col == 1 ] != 0 : NEW_LINE INDENT return dp [ mask ] [ prev ] [ col == 1 ] NEW_LINE DEDENT ans = int ( 1e9 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE DEDENT