If the state is already calculated .
if dp [ ind ] [ st ] != - 1 : NEW_LINE INDENT return dp [ ind ] [ st ] NEW_LINE DEDENT if not st : NEW_LINE INDENT dp [ ind ] [ st ] = max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , ( findlength ( arr , s , n , ind + 1 , 0 , dp ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ ind ] [ st ] = max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , 0 ) NEW_LINE DEDENT return dp [ ind ] [ st ] NEW_LINE