Calculate the minimum sum of products for K subsets
for ( int i = 1 ; i < K ; i ++ ) { for ( int j = len - 1 ; j >= i ; j -- ) { count_zero = 0 ; count_one = 0 ; dp [ j ] = Int32 . MaxValue ; for ( int k = j ; k >= i ; k -- ) { if ( S [ k ] == '0' ) { count_zero ++ ; } else { count_one ++ ; } dp [ j ] = Math . Min ( dp [ j ] , count_zero * count_one + dp [ k - 1 ] ) ; } } } return dp [ len - 1 ] ; }