Filling first row , if only 1 bucket then simple count number of zeros and ones and do the multiplication
zeroes = 0 NEW_LINE ones = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( Str [ i ] == '0' ) : NEW_LINE INDENT zeroes += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ones += 1 NEW_LINE DEDENT dp [ 0 ] [ i ] = ones * zeroes NEW_LINE DEDENT for s in range ( 1 , K ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT dp [ s ] [ i ] = sys . maxsize NEW_LINE ones = 0 NEW_LINE zeroes = 0 NEW_LINE for k in range ( i , - 1 , - 1 ) : NEW_LINE INDENT if ( Str [ k ] == '0' ) : NEW_LINE INDENT zeroes += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ones += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT