Problem p03930 (python)

Fixed submission:
from copy import deepcopy
H, W, K = map(int, input().split())

def clear(row, start, end, mult):
    if end - start + 1 < K:
        return 0
    gain = 0
    for i in range(start, end + 1):
        gain += mult * row[i]
        row[i] = 0
    return gain

def drop(board):
    for x in range(W):
        wp = 0
        for y in range(H):
            if board[y][x] > 0:
                board[wp][x] = board[y][x]
                if wp != y:
                    board[y][x] = 0
                wp += 1

def proceed(board, mult):
    gain = 0
    for row in board:
        start = -1
        num = -1
        for i in range(W):
            if row[i] == num:
                continue
            else:
                gain += clear(row, start, i-1, mult)
                num = row[i]
                start = i
        if num > 0:
            gain += clear(row, start, W-1, mult)
    drop(board)
    return gain

board_ = []
for _ in range(H):
    board_.append(list(map(int, input())))
board_.reverse()

ans = 0
for y in range(H):
    for x in range(W):
        board = deepcopy(board_)
        board[y][x] = 0
        drop(board)
        mult = 1
        cand = 0
        while True:
            gain = proceed(board, mult)
            if gain == 0:
                break
            cand += gain
            mult *= 2
        ans = max(ans, cand)
print(ans)

Buggy submission (verdict: Runtime Error):
H, W, K = map(int, input().split())

def clear(row, start, end, mult):
    if end - start + 1 < K:
        return 0
    gain = 0
    for i in range(start, end + 1):
        gain += mult * row[i]
        row[i] = 0
    return gain

def drop(board):
    for x in range(W):
        wp = 0
        for y in range(H):
            if board[y][x] > 0:
                board[wp][x] = board[y][x]
                if wp != y:
                    board[y][x] = 0
                wp += 1

def proceed(board, mult):
    gain = 0
    for row in board:
        start = -1
        num = -1
        for i in range(W):
            if row[i] == num:
                continue
            else:
                gain += clear(row, start, i-1, mult)
                num = row[i]
                start = i
        if num > 0:
            gain += clear(row, start, W-1, mult)
    drop(board)
    return gain

board_ = []
for _ in range(H):
    board_.append(list(map(int, input())))
board_.reverse()

ans = 0
for y in range(H):
    for x in range(W):
        board = deepcopy(board_)
        board[y][x] = 0
        drop(board)
        mult = 1
        cand = 0
        while True:
            gain = proceed(board, mult)
            if gain == 0:
                break
            cand += gain
            mult *= 2
        ans = max(ans, cand)
print(ans)

Buggy submission (verdict: Runtime Error):
H, W, K = map(int, input().split())

def clear(row, start, end, mult):
    if end - start + 1 < K:
        return 0
    gain = 0
    for i in range(start, end + 1):
        gain += mult * row[i]
        row[i] = 0
    return gain

def drop(board):
    for x in range(W):
        wp = 0
        for y in range(H):
            if board[y][x] > 0:
                board[wp][x] = board[y][x]
                if wp != y:
                    board[y][x] = 0
                wp += 1

def proceed(board, mult):
    gain = 0
    for row in board:
        start = -1
        num = -1
        for i in range(W):
            if row[i] == num:
                continue
            else:
                gain += clear(row, start, i-1, mult)
                num = row[i]
                start = i
        if num > 0:
            gain += clear(row, start, W-1, mult)
    drop(board)
    return gain

board_ = []
for _ in range(H):
    board_.append(list(map(int, input())))
board_.reverse()

ans = 0
for y in range(H):
    for x in range(W):
        board = deepcopy(board_)
        board[y][x] = 0
        drop(board)
        mult = 1
        cand = 0
        while True:
            gain = proceed(board, mult)
            if gain == 0:
                break
            cand += gain
            mult *= 2
        ans = max(ans, cand)
print(ans)