Problem p02695 (python)

Fixed submission:
#!/usr/bin/env python3
import sys

N=0
M=0

def next_num(x):
    if int(x[-1]) < M:
        return x[:-1] + [str(int(x[-1]) + 1)]
    elif int(x[-1]) == M:
        oya = next_num(x[:-1])
        return oya + [oya[-1]]


def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    start = ['1' for i in range(N)]
    current = start
    ans = 0
    while True:
        result = 0
        for a_, b_, c_, d_ in zip(a, b, c, d):
            if int(current[b_]) - int(current[a_]) == c_:
                result += d_
        if result > ans:
            ans = result
        if current[0] == str(M):
            break
        current = next_num(current)
    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    global N, M
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (Q)  # type: "List[int]"
    b = [int()] * (Q)  # type: "List[int]"
    c = [int()] * (Q)  # type: "List[int]"
    d = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        a[i] = int(next(tokens)) - 1
        b[i] = int(next(tokens)) - 1
        c[i] = int(next(tokens))
        d[i] = int(next(tokens))
    solve(N, M, Q, a, b, c, d)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys

N=0
M=0

def next_num(x):
    if int(x[-1]) < M:
        return x[:-1] + [int(x[-1]) + 1]
    elif int(x[-1]) == M:
        oya = next_num(x[:-1])
        return oya + [oya[-1]]


def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    start = ['1' for i in range(N)]
    current = start
    ans = 0
    while True:
        result = 0
        for a_, b_, c_, d_ in zip(a, b, c, d):
            if int(current[b_]) - int(current[a_]) == c_:
                result += d_
        if result > ans:
            ans = result
        if current[0] == M:
            break
        current = next_num(current)
    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    global N, M
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (Q)  # type: "List[int]"
    b = [int()] * (Q)  # type: "List[int]"
    c = [int()] * (Q)  # type: "List[int]"
    d = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        a[i] = int(next(tokens)) - 1
        b[i] = int(next(tokens)) - 1
        c[i] = int(next(tokens))
        d[i] = int(next(tokens))
    solve(N, M, Q, a, b, c, d)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys

N=0
M=0

def next_num(x,  M):
    if int(x[-1]) < M:
        return x[:-1] + [int(x[-1]) + 1]
    elif int(x[-1]) == M:
        oya = next_num(x[:-1], M)
        return oya + [oya[-1]]


def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    start = ['1' for i in range(N)]
    current = start
    ans = 0
    while True:
        result = 0
        for a_, b_, c_, d_ in zip(a, b, c, d):
            if int(current[b_]) - int(current[a_]) == c_:
                result += d_
        if result > ans:
            ans = result
        if current[0] == M:
            break
        current = next_num(current, M)
    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (Q)  # type: "List[int]"
    b = [int()] * (Q)  # type: "List[int]"
    c = [int()] * (Q)  # type: "List[int]"
    d = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        a[i] = int(next(tokens)) - 1
        b[i] = int(next(tokens)) - 1
        c[i] = int(next(tokens))
        d[i] = int(next(tokens))
    solve(N, M, Q, a, b, c, d)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys

N=0
M=0

def next_num(x,  M):
    if x == [str(M)] * N:
        return -1
    if int(x[-1]) < M:
        return x[:-1] + [int(x[-1]) + 1]
    elif int(x[-1]) == M:
        oya = next_num(x[:-1], M)
        return oya + [oya[-1]]


def solve(N: int, M: int, Q: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    start = ['1' for i in range(N)]
    current = start
    ans = 0
    while current[0] != M:
        result = 0
        for a_, b_, c_, d_ in zip(a, b, c, d):
            if int(current[b_]) - int(current[a_]) == c_:
                result += d_
        if result > ans:
            ans = result
        current = next_num(current, M)
    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (Q)  # type: "List[int]"
    b = [int()] * (Q)  # type: "List[int]"
    c = [int()] * (Q)  # type: "List[int]"
    d = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        a[i] = int(next(tokens)) - 1
        b[i] = int(next(tokens)) - 1
        c[i] = int(next(tokens))
        d[i] = int(next(tokens))
    solve(N, M, Q, a, b, c, d)

if __name__ == '__main__':
    main()