Calculate the minimum sum of products for K subsets
for i in range ( 1 , K ) : NEW_LINE INDENT for j in range ( Len - 1 , i - 1 , - 1 ) : NEW_LINE INDENT count_zero = 0 ; NEW_LINE count_one = 0 ; NEW_LINE dp [ j ] = sys . maxsize ; NEW_LINE for k in range ( j , i - 1 , - 1 ) : NEW_LINE INDENT if ( S [ k ] == '0' ) : NEW_LINE INDENT count_zero += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT count_one += 1 ; NEW_LINE DEDENT dp [ j ] = min ( dp [ j ] , count_zero * count_one + dp [ k - 1 ] ) ; NEW_LINE DEDENT DEDENT DEDENT return dp [ Len - 1 ] ; NEW_LINE