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):
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline
    INF = 10**9

    H, W, K = map(int, input().split())
    chocolate = [ input()[:-1] for _ in range(H) ]
    
    loop_H = range(H)
    loop_W = range(W)
    white_chocolate = [ [0]*(W+1) for _ in range(10) ]
    min_cut = INF-1
    for div in range(1 << (H-1)):
        
        group = [[]]
        group_number = 0
        for i in loop_H:
            group[group_number].append(i)
            if div >> i & 1:
                group.append([])
                group_number += 1
        
        for i in loop_W:
            for g in range(group_number+1):
                white_chocolate[g][i] = 0
                for j in group[g]:
                    if chocolate[j][i] == '1':
                        white_chocolate[g][i] += 1
        
        cut = group_number
        zeros = [0] * len(group_number+1)
        now = zeros.copy()
        for w in loop_W:
            for g in range(len(white_chocolate)):
                if white_chocolate[g][w] > K:
                    cut = INF
                    break
                else:
                    now[g] += white_chocolate[g][w]
                if now[g] + white_chocolate[g][w+1] > K:
                    cut += 1
                    now = zeros.copy()
                    break
            if cut == INF:
                break
        if cut < min_cut:
            min_cut = cut
    print(min_cut)

main()

def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline
    INF = 10**9

    H, W, K = map(int, input().split())
    chocolate = [ input()[:-1] for _ in range(H) ]
    
    loop_H = range(H)
    loop_W = range(W)
    min_cut = INF-1
    for div in range(1 << (H-1)):
        
        group = [[]]
        group_number = 0
        for i in loop_H:
            group[group_number].append(i)
            if div >> i & 1:
                group.append([])
                group_number += 1
        
        cut = group_number
        zeros = [0] * (group_number + 1)
        now = zeros.copy()
        for w in loop_W:
            for g in range(len(group)):
                cnt = 0
                for i in group[g]:
                    if chocolate[i][w] == '1':
                        cnt += 1
                if cnt > K:
                    cut = INF
                    break
                if now[g] + cnt > K:
                    cut += 1
                    now = zeros.copy()
                    break
                now[g] += cnt
            if cut == INF:
                break
        if cut < min_cut:
            min_cut = cut
    print(min_cut)

main()

def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline
    INF = 10**9

    H, W, K = map(int, input().split())
    chocolate = [ input()[:-1] for _ in range(H) ]
    
    loop_H = range(H)
    loop_W = range(W)
    white_chocolate = [ [0]*(W+1) for _ in range(group_number+1) ]
    min_cut = INF-1
    for div in range(1 << (H-1)):
        
        group = [[]]
        group_number = 0
        for i in loop_H:
            group[group_number].append(i)
            if div >> i & 1:
                group.append([])
                group_number += 1
        
        for i in loop_W:
            for g in range(group_number+1):
                white_chocolate[g][i] = 0
                for j in group[g]:
                    if chocolate[j][i] == '1':
                        white_chocolate[g][i] += 1
        
        cut = group_number
        zeros = [0] * len(white_chocolate)
        now = zeros.copy()
        for w in loop_W:
            for g in range(len(white_chocolate)):
                if white_chocolate[g][w] > K:
                    cut = INF
                    break
                else:
                    now[g] += white_chocolate[g][w]
                if now[g] + white_chocolate[g][w+1] > K:
                    cut += 1
                    now = zeros.copy()
                    break
            if cut == INF:
                break
        if cut < min_cut:
            min_cut = cut
    print(min_cut)

main()

def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline
    INF = 10**9

    H, W, K = map(int, input().split())
    chocolate = [ input() for _ in range(H) ]
    
    loop_H = range(H)
    min_cut = INF-1
    for div in range(1 << (H-1)):
        
        group = [[]]
        group_number = 0
        for i in loop_H:
            group[group_number].append(i)
            if div >> i & 1:
                group.append([])
                group_number += 1
        
        cut = group_number
        cnt = [0] * len(group)
        w = 0
        while w < W:
            now = [0] * len(group)
            for g in range(len(group)):
                for i in group[g]:
                    if chocolate[i][w] == '1':
                        now[g] += 1
                if now[g] > K:
                    cut = INF
                    break
                if cnt[g] + now[g] > K:
                    cut += 1
                    w -= 1
                    cnt = [0] * len(group)
                    break
                cnt[g] += now[g]
            if cut >= min_cut:
                break
            w += 1
        if cut < min_cut:
            min_cut = cut
    print(min_cut)

main()

Verdicts: Runtime Error

Wrong Answer

Runtime Error

Accepted