Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
a_list = list(map(int, input().split()))

def divisors(num):
    """
    約数全列挙
    """
    divisors = []
    for i in range(1, int(num ** 0.5) + 1):
        if num % i == 0:
            divisors.append(i)
            if i != num // i:
                divisors.append(num // i)

    return divisors
sum_num = sum(a_list)
a_list.sort()
div = divisors(sum_num)
div.sort(reverse=True)
ans = 1
from bisect import *
for d in div:
    plus = 0
    minus = 0
    mul = [-d]
    if d == 1:
        break
    for i in range(22):
        mul.append(d*i)
    for i, a in enumerate(a_list):
        if i != N-1:
            if a % d == 0:
                continue
            else:
                idx = bisect_left(mul, a)
                pp = mul[idx-1]
                p = mul[idx]
                n = mul[idx+1]
                v = min(abs(a-pp), abs(a-p), abs(a-n))

                if plus + v <= K and minus + v <= K:
                    for i in [pp, p, n]:
                        if abs(a-i) == v:
                            if i >= a:
                                plus += i - a
                            else:
                                minus += a -i
                else:
                    if plus + v >= K:
                        s = float("inf")
                        for i in [pp, p, n]:
                            if abs(a-i) != v:
                                s = min(s, abs(a-i))
                        minus += s
                    else:
                        s = float("inf")
                        for i in [pp, p, n]:
                            if abs(a-i) != v:
                                s = min(s, abs(a-i))
                        plus += s
        else:
            if plus == minus and a % d == 0:
                ans = max(ans, d)
                break
            else:
                if plus >= minus:
                    tmp = plus - minus
                    if plus > K :
                        continue
                    if (a + tmp) % d == 0:
                        ans = max(ans, d)
                        break
                else:
                    tmp = minus - plus
                    if minus > K:
                        continue
                    if (a + tmp) % d == 0:
                        ans = max(ans, d)
                        break
print(ans)


Fixed submission:
N, K = map(int, input().split())
a_list = list(map(int, input().split()))

def divisors(num):
    """
    約数全列挙
    """
    divisors = []
    for i in range(1, int(num ** 0.5) + 1):
        if num % i == 0:
            divisors.append(i)
            if i != num // i:
                divisors.append(num // i)

    return divisors
sum_num = sum(a_list)
a_list.sort()
div = divisors(sum_num)
div.sort(reverse=True)
ans = 1
from itertools import accumulate
for d in div:
    mod = []
    if d == 1:
        break
    for a in a_list:
        mod.append(a % d)
    mod.sort()
    acc_m = list(accumulate(mod))
    modd = []
    for i in range(N):
        modd.append(d-mod[i])
    acc_p = list(accumulate(modd))
    flag = False
    for i in range(N-1):
        p_sum = acc_p[-1] - acc_p[i]
        n_sum = acc_m[i]
        if p_sum == n_sum and p_sum <= K:
            flag = True
    if flag:
        ans = d
        break

print(ans)