Problem p02733 (python)

Fixed submission:
def main():
    H,W,K = map(int,input().split())
    S = [input()for _ in range(H)]
    ans = 10**9
    for i in range(2**(H-1)):
        aa = 0
        for j in range(H-1):
            if (i>>j)&1 == 1:
                aa+=1
        c = aa
        b = [0]*(c+1)
        OK = True
        for j in range(W):
            k = 0
            for h in range(H):
                if S[h][j] == "1":b[k] += 1
                if (i>>h)&1==1:k+=1
            
            ok = True
            for p in b:
                if p > K:ok = False
            if not ok:
                aa += 1
                b = [0]*(c+1)
                
                k = 0
                for h in range(H):
                    if S[h][j]=="1":b[k] += 1
                    if (i>>h)&1==1:k+=1
                ok = True
                for p in b:
                    if p > K:ok = False
                if not ok:OK = False;break                
        if OK:ans = min(ans,aa)
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    H,W,K = map(int,input().split())
    S = [[int(s) for s in input()]for _ in range(H)]
    ans = 2**60
    for i in range(2**(H-1)):
        aa = 0
        for j in range(H-1):
            if (i>>j)&1 == 1:
                aa+=1
        c = aa
        b = [0]*(c+1)
        OK = True
        for j in range(W):
            k = 0
            for h in range(H):
                if S[h][j] == 1:b[k] += 1
                if (i>>j)&1==1:k+=1
            
            ok = True
            for p in b:
                if p > K:ok = False
            if not ok:
                aa += 1
                b = [0]*(c+1)
                k = 0
                for h in range(H):
                    if S[h][j]==1:b[k] += 1
                    if (i>>j)&1==1:k+=1
                ok = True
                for p in b:
                    if p > K:ok = False
                if not ok:OK = False;break                
        if OK:ans = min(ans,aa)
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    H,W,K = map(int,input().split())
    S = [[int(s) for s in input()]for _ in range(H)]
    ans = 10**9
    for i in range(2**(H-1)):
        aa = 0
        for j in range(H-1):
            if (i>>j)&1 == 1:
                aa+=1
        c = aa
        b = [0 for _ in range(c+1)]
        OK = True
        for j in range(W):
            k = 0
            for h in range(H):
                if S[h][j] == 1:b[k] += 1
                if (i>>j)&1==1:k+=1
            
            ok = True
            for p in b:
                if p > K:ok = False
            if not ok:
                aa += 1
                b = [0]*(c+1)
                k = 0
                for h in range(H):
                    if S[h][j]==1:b[k] += 1
                    if (i>>j)&1==1:k+=1
            ok = True
            for p in b:
                if p > K:ok = False
            if not ok:OK = False;break                
        if OK:ans = min(ans,aa)
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    H,W,K = map(int,input().split())
    S = [[int(s) for s in input()]for _ in range(H)]
    ans = 2**60
    for i in range(2**(H-1)):
        aa = 0
        for j in range(H-1):
            if (i>>j)&1 == 1:
                aa+=1
        c = aa
        b = [0 for _ in range(c+1)]
        OK = True
        for j in range(W):
            k = 0
            for h in range(H):
                if S[h][j] == 1:b[k] += 1
                if (i>>j)&1==1:k+=1
            
            ok = True
            for p in b:
                if p > K:ok = False
            if not ok:
                aa += 1
                b = [0]*(c+1)
                k = 0
                for h in range(H):
                    if S[h][j]==1:b[k] += 1
                    if (i>>j)&1==1:k+=1
            ok = True
            for p in b:
                if p > K:ok = False
            if not ok:OK = False;break                
        if OK:ans = min(ans,aa)
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque
def main():
    H,W,K = map(int,input().split())
    S = [[int(s) for s in input()]for _ in range(H)]
    ans = 2**60
    for i in range(2**(H-1)):
        aa = 0
        for j in range(H-1):
            if (i>>j)&1 == 1:
                aa+=1
        c = aa
        b = [0 for _ in range(c+1)]
        OK = True
        for j in range(W):
            k = 0
            for h in range(H):
                if S[h][j]:b[k] += 1
                if (i>>j)&1==1:k+=1
            
            ok = True
            for p in b:
                if p > K:ok = False
            if not ok:
                aa += 1
                b = [0]*(c+1)
                k = 0
                for h in range(H):
                    if S[h][j]:b[k] += 1
                    if (i>>j)&1==1:k+=1
            ok = True
            for p in b:
                if p > K:ok = False
            if not ok:OK = False;break                
    if OK:ans = min(ans,aa)
    print(ans)

if __name__ == "__main__":
    main()