Fill the dp [ ] for base case when all elements are selected from B [ ]
for i in range ( 1 , l2 + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + B [ i - 1 ] NEW_LINE max_sum = max ( max_sum , dp [ 0 ] [ i ] ) NEW_LINE DEDENT for i in range ( 1 , l1 + 1 ) : NEW_LINE INDENT for j in range ( 1 , l2 + 1 ) : NEW_LINE INDENT if ( i + j <= l ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] + A [ i + j - 1 ] , dp [ i ] [ j - 1 ] + B [ i + j - 1 ] ) NEW_LINE DEDENT max_sum = max ( dp [ i ] [ j ] , max_sum ) NEW_LINE DEDENT DEDENT