Problem: We have a chocolate bar partitioned into H horizontal rows and W vertical columns of squares.  The square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is `0`, and white if S_{i,j} is `1`.  We will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar.  How many times do we need to cut the bar so that every block after the cuts has K or less white squares?

Submission attempts (in order):
import numpy as np
import itertools

H, W, K = map(int, input().split())
choco = []
for i in range(H):
    choco.append([int(x) for x in list(input())])

arr = np.array(choco)


count = (H-1) + (W-1)

for k in range(H):
    
    
    if count <= k:
        break

    
    for comb in itertools.combinations(range(1,H),k):

        
        t = (None,) + comb + (None,)
        s = [slice(t[j],t[j+1],) for j in range(len(t)-1)]
        
        # print(t)
        
        sum_p = [0] * (k+1)
        
        cut = k
        
        for col in zip(*choco):
                        
            
            sum_a = [sum(col[b]) for b in s]
            # print(sum_a)
            
            if any(map(K.__lt__, sum_a)):
                
                break
            
            sum_b = [x+y for x,y in zip(sum_p, sum_a)]

            if any(map(K.__lt__, sum_b)):
                cut += 1
                sum_p = [0] * (k+1)
            else:
                sum_p = sum_b
                
                
        
        else:
            count = min(count,cut)

print(count)

import numpy as np
import itertools

H, W, K = map(int, input().split())
choco = []
for i in range(H):
    choco.append([int(x) for x in list(input())])

arr = np.array(choco)


count = (H-1) + (W-1)

for cho in choco:
    print(cho)

for k in range(H):
    print(k)
    
    
    if count <= k:
        break

    
    for comb in itertools.combinations(range(1,H),k):

        
        t = (None,) + comb + (None,)
        s = [slice(t[j],t[j+1],) for j in range(len(t)-1)]
        
        # print(t)
        
        sum_p = [0] * (k+1)
        
        cut = k
        
        for col in zip(*choco):

            
            sum_a = [sum(col[b]) for b in s]
            
            
            if any(map(K.__lt__, sum_a)):
                
                break
            
            
            
            sum_b = [x + y for x,y in zip(sum_p, sum_a)]
            print(k, sum_b)

            if any(map(K.__lt__, sum_b)):
                cut += 1
                sum_p = sum_a
            else:
                sum_p = sum_b
                
                
        
        else:
            count = min(count,cut)

print(count)

import numpy as np
import itertools

H, W, K = map(int, input().split())
choco = []
for i in range(H):
    choco.append([int(x) for x in list(input())])

arr = np.array(choco)


print(H, W, K)
count = (H-1) + (W-1)

for k in range(H):
    
    
    if count <= k:
        break

    
    for comb in itertools.combinations(range(1,H),k):

        
        t = (None,) + comb + (None,)
        s = [slice(t[j],t[j+1],) for j in range(len(t)-1)]
        
        # print(t)
        
        sum_p = [0] * (k+1)
        
        cut = k
        
        for col in zip(*choco):
                        
            
            sum_a = [sum(col[b]) for b in s]
            # print(sum_a)
            
            if any(map(K.__lt__, sum_a)):
                
                break
            
            sum_b = [x+y for x,y in zip(sum_p, sum_a)]

            if any(map(K.__lt__, sum_b)):
                cut += 1
                sum_p = [0] * (k+1)
            else:
                sum_p = sum_b
                
                
        
        else:
            count = min(count,cut)

print(count)

import numpy as np
import itertools

H, W, K = map(int, input().split())
choco = []
for i in range(H):
    choco.append([int(x) for x in list(input())])

arr = np.array(choco)


count = (H-1) + (W-1)


for k in range(H):
    
    
    if count <= k:
        break

    
    for comb in itertools.combinations(range(1,H),k):

        
        t = (None,) + comb + (None,)
        s = [slice(t[j],t[j+1],) for j in range(len(t)-1)]
        
        sum_p = [0] * (k+1)
        
        cut = k
        
        for col in zip(*choco):

            
            sum_a = [sum(col[b]) for b in s]
            
            
            if any(map(K.__lt__, sum_a)):
                
                break
            
            
            
            sum_b = [x + y for x,y in zip(sum_p, sum_a)]

            if any(map(K.__lt__, sum_b)):
                cut += 1
                sum_p = sum_a
            else:
                sum_p = sum_b
                
                
        
        else:
            count = min(count,cut)

print(count)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted