Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    from math import sqrt, ceil
    N,K = LMIIS()
    A = LMIIS()
    suma = sum(A)
    dL = [0] * N
    dU = [0] * N
    ds = []
    # for x in range(1,ceil(sqrt(suma))+1):
    for x in range(1,suma):
        
        if suma % x != 0:
            continue

        d = suma // x 

        ds.append(x)
        for i in range(N):
            dL[i] = A[i] % d
        dL.sort()
        for i in range(N):
            dU[i] = d - dL[i]

        c = 0
        l = 0
        r = N-1
        while l < r:
            #print(l,r)
            if dL[l] == dU[r]:
                c += dL[l]
                if c > K:
                    break
                l += 1
                r -= 1
                if l >= N or r < 0:
                    break
            elif dL[l] < dU[r]:
                c += dL[l]
                if c > K:
                    break
                dU[r] -= dL[l]
                l += 1
                if l >= N:
                    break
            else:
                c += dU[r]
                if c > K:
                    break
                dL[l] -= dU[r]
                r -= 1
                if r < 0:
                    break
            
            
        if l >= r:
            print(d)
            return

    for i  in range(len(ds)-1,-1,-1):
        d = ds[i]
        for i in range(N):
            dL[i] = A[i] % d
        dL.sort()
        for i in range(N):
            dU[i] = d - dL[i]

        c = 0
        l = 0
        r = N-1
        while l < r:
            #print(l,r)
            if dL[l] == dU[r]:
                c += dL[l]
                if c > K:
                    break
                l += 1
                r -= 1
                if l >= N or r < 0:
                    break
            elif dL[l] < dU[r]:
                c += dL[l]
                if c > K:
                    break
                dU[r] -= dL[l]
                l += 1
                if l >= N:
                    break
            else:
                c += dU[r]
                if c > K:
                    break
                dL[l] -= dU[r]
                r -= 1
                if r < 0:
                    break
            
            
        if l >= r:
            print(d)
            return


if __name__ == '__main__':
    main()

Fixed submission:
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    from math import sqrt, ceil
    N,K = LMIIS()
    A = LMIIS()
    suma = sum(A)
    dL = [0] * N
    dU = [0] * N
    ds = []
    for x in range(1,ceil(sqrt(suma))+1):
        
        if suma % x != 0:
            continue

        d = suma // x 
        ds.append(x)
        for i in range(N):
            dL[i] = A[i] % d
        dL.sort()
        for i in range(N):
            dU[i] = d - dL[i]

        c = 0
        l = 0
        r = N-1
        while l < r:
            #print(l,r)
            if dL[l] == dU[r]:
                c += dL[l]
                if c > K:
                    break
                l += 1
                r -= 1
                if l >= N or r < 0:
                    break
            elif dL[l] < dU[r]:
                c += dL[l]
                if c > K:
                    break
                dU[r] -= dL[l]
                l += 1
                if l >= N:
                    break
            else:
                c += dU[r]
                if c > K:
                    break
                dL[l] -= dU[r]
                r -= 1
                if r < 0:
                    break
            
            
        if l >= r:
            print(d)
            return

    for i  in range(len(ds)-1,-1,-1):
        d = ds[i]
        for i in range(N):
            dL[i] = A[i] % d
        dL.sort()
        for i in range(N):
            dU[i] = d - dL[i]

        c = 0
        l = 0
        r = N-1
        while l < r:
            #print(l,r)
            if dL[l] == dU[r]:
                c += dL[l]
                if c > K:
                    break
                l += 1
                r -= 1
                if l >= N or r < 0:
                    break
            elif dL[l] < dU[r]:
                c += dL[l]
                if c > K:
                    break
                dU[r] -= dL[l]
                l += 1
                if l >= N:
                    break
            else:
                c += dU[r]
                if c > K:
                    break
                dL[l] -= dU[r]
                r -= 1
                if r < 0:
                    break
            
            
        if l >= r:
            print(d)
            return


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():

    N,K = LMIIS()
    A = LMIIS()
    suma = sum(A)
    dL = [0] * N
    dU = [0] * N
    for x in range(1,suma+1):
        
        if suma % x != 0:
            continue

        d = suma // x 
        for i in range(N):
            dL[i] = A[i] % d
        dL.sort()
        for i in range(N):
            dU[i] = d - dL[i]

        c = 0
        l = 0
        r = N-1
        while l < r:
            #print(l,r)
            if dL[l] == dU[r]:
                c += dL[l]
                if c > K:
                    break
                l += 1
                r -= 1
                if l >= N or r < 0:
                    break
            elif dL[l] < dU[r]:
                c += dL[l]
                if c > K:
                    break
                dU[r] -= dL[l]
                l += 1
                if l >= N:
                    break
            else:
                c += dU[r]
                if c > K:
                    break
                dL[l] -= dU[r]
                r -= 1
                if r < 0:
                    break
            
            
        if l >= r:
            print(d)
            return

if __name__ == '__main__':
    main()

Fixed submission:
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    from math import sqrt, ceil
    N,K = LMIIS()
    A = LMIIS()
    suma = sum(A)
    dL = [0] * N
    dU = [0] * N
    ds = []
    for x in range(1,ceil(sqrt(suma))+1):
        
        if suma % x != 0:
            continue

        d = suma // x 
        ds.append(x)
        for i in range(N):
            dL[i] = A[i] % d
        dL.sort()
        for i in range(N):
            dU[i] = d - dL[i]

        c = 0
        l = 0
        r = N-1
        while l < r:
            #print(l,r)
            if dL[l] == dU[r]:
                c += dL[l]
                if c > K:
                    break
                l += 1
                r -= 1
                if l >= N or r < 0:
                    break
            elif dL[l] < dU[r]:
                c += dL[l]
                if c > K:
                    break
                dU[r] -= dL[l]
                l += 1
                if l >= N:
                    break
            else:
                c += dU[r]
                if c > K:
                    break
                dL[l] -= dU[r]
                r -= 1
                if r < 0:
                    break
            
            
        if l >= r:
            print(d)
            return

    for i  in range(len(ds)-1,-1,-1):
        d = ds[i]
        for i in range(N):
            dL[i] = A[i] % d
        dL.sort()
        for i in range(N):
            dU[i] = d - dL[i]

        c = 0
        l = 0
        r = N-1
        while l < r:
            #print(l,r)
            if dL[l] == dU[r]:
                c += dL[l]
                if c > K:
                    break
                l += 1
                r -= 1
                if l >= N or r < 0:
                    break
            elif dL[l] < dU[r]:
                c += dL[l]
                if c > K:
                    break
                dU[r] -= dL[l]
                l += 1
                if l >= N:
                    break
            else:
                c += dU[r]
                if c > K:
                    break
                dL[l] -= dU[r]
                r -= 1
                if r < 0:
                    break
            
            
        if l >= r:
            print(d)
            return


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():

    N,K = LMIIS()
    A = LMIIS()
    suma = sum(A)
    for x in range(1,suma+1):
        
        if suma % x != 0:
            continue
        d = suma // x
    
        dL = [0] * N
        dU = [0] * N
        for i in range(N):
            dL[i] = A[i] % d
            dU[i] = d - A[i] % d

        dL.sort()
        dU.sort(reverse=True)
        c = 0
        l = 0
        r = N-1
        while l < r:
            #print(l,r)
            if dL[l] == dU[r]:
                c += dL[l]
                if c > K:
                    break
                l += 1
                r -= 1
                if l >= N or r < 0:
                    break
            elif dL[l] < dU[r]:
                c += dL[l]
                if c > K:
                    break
                dU[r] -= dL[l]
                l += 1
                if l >= N:
                    break
            else:
                c += dU[r]
                if c > K:
                    break
                dL[l] -= dU[r]
                r -= 1
                if r < 0:
                    break
            
            
        if l >= r:
            print(d)
            return

if __name__ == '__main__':
    main()

Fixed submission:
import sys
sys.setrecursionlimit(2**31-1)
input = sys.stdin.readline
write = sys.stdout.write
LMIIS = lambda : list(map(int,input().split()))
II = lambda : int(input())
dbg = lambda *something : print(*something) if DEBUG is True else 0
DEBUG = True

def main():
    from math import sqrt, ceil
    N,K = LMIIS()
    A = LMIIS()
    suma = sum(A)
    dL = [0] * N
    dU = [0] * N
    ds = []
    for x in range(1,ceil(sqrt(suma))+1):
        
        if suma % x != 0:
            continue

        d = suma // x 
        ds.append(x)
        for i in range(N):
            dL[i] = A[i] % d
        dL.sort()
        for i in range(N):
            dU[i] = d - dL[i]

        c = 0
        l = 0
        r = N-1
        while l < r:
            #print(l,r)
            if dL[l] == dU[r]:
                c += dL[l]
                if c > K:
                    break
                l += 1
                r -= 1
                if l >= N or r < 0:
                    break
            elif dL[l] < dU[r]:
                c += dL[l]
                if c > K:
                    break
                dU[r] -= dL[l]
                l += 1
                if l >= N:
                    break
            else:
                c += dU[r]
                if c > K:
                    break
                dL[l] -= dU[r]
                r -= 1
                if r < 0:
                    break
            
            
        if l >= r:
            print(d)
            return

    for i  in range(len(ds)-1,-1,-1):
        d = ds[i]
        for i in range(N):
            dL[i] = A[i] % d
        dL.sort()
        for i in range(N):
            dU[i] = d - dL[i]

        c = 0
        l = 0
        r = N-1
        while l < r:
            #print(l,r)
            if dL[l] == dU[r]:
                c += dL[l]
                if c > K:
                    break
                l += 1
                r -= 1
                if l >= N or r < 0:
                    break
            elif dL[l] < dU[r]:
                c += dL[l]
                if c > K:
                    break
                dU[r] -= dL[l]
                l += 1
                if l >= N:
                    break
            else:
                c += dU[r]
                if c > K:
                    break
                dL[l] -= dU[r]
                r -= 1
                if r < 0:
                    break
            
            
        if l >= r:
            print(d)
            return


if __name__ == '__main__':
    main()