Problem:
 Score : 500 points    Problem Statement We have a sequence of N integers: A_1, A_2, \cdots, A_N .  You can perform the following operation between 0 and K times (inclusive):   Choose two integers i and j such that i \neq j , each between 1 and N (inclusive). Add 1 to A_i and -1 to A_j , possibly producing a negative element.   Compute the maximum possible positive integer that divides every element of A after the operations. Here a positive integer x divides an integer y if and only if there exists an integer z such that y = xz .      Constraints  2 \leq N \leq 500  1 \leq A_i \leq 10^6  0 \leq K \leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  K  A_1  A_2  \cdots  A_{N-1}  A_{N}       Output Print the maximum possible positive integer that divides every element of A after the operations.        Sample Input 1 2 3
8 20      Sample Output 1 7  7 will divide every element of A if, for example, we perform the following operation:   Choose i = 2, j = 1 . A becomes (7, 21) .   We cannot reach the situation where 8 or greater integer divides every element of A .       Sample Input 2 2 10
3 5      Sample Output 2 8  Consider performing the following five operations:   Choose i = 2, j = 1 . A becomes (2, 6) .  Choose i = 2, j = 1 . A becomes (1, 7) .  Choose i = 2, j = 1 . A becomes (0, 8) .  Choose i = 2, j = 1 . A becomes (-1, 9) .  Choose i = 1, j = 2 . A becomes (0, 8) .   Then, 0 = 8 \times 0 and 8 = 8 \times 1 , so 8 divides every element of A . We cannot reach the situation where 9 or greater integer divides every element of A .       Sample Input 3 4 5
10 1 2 22      Sample Output 3 7       Sample Input 4 8 7
1 7 5 6 8 2 6 5      Sample Output 4 5

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def divisors(n):
    S,T=[],[]
    for i in range(1,int(n**.5)+1):
        if n%i==0:
            S.append(i)
            T.append(n//i)
    T.reverse()
    return S+T if S[-1]!=T[0] else S+T[1:]

def resolve():
    n,k=map(int,input().split())
    A=list(map(int,input().split()))
    D=divisors(sum(A))
    def check(d):
        P=[]
        N=[]
        for i in range(n):
            r=A[i]%d
            if(not r): continue
            P.append(d-r)
        P.sort()
        l=len(P)
        N=[d-p for p in P]
        N.reverse()
        PS=[0]*(l+1)
        NS=[0]*(l+1)
        for i in range(l):
            PS[i+1]=PS[i]+P[i]
            NS[i+1]=NS[i]+N[i]
        NS.reverse()
        res=INF
        for i in range(l):
            res=min(res,max(PS[i],NS[i+1]))
        return res<=k
    ans=1
    for d in D:
        if(check(d)): ans=d
    print(ans)
resolve()

Solution (Python):
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort(reverse = True)
    return divisors

import heapq

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

inf = float('inf')
ans = int(0)

N, K = LI()
A = LI()
cou = int(0)
sumA = sum(A)
anscandi = make_divisors(sumA)


for i in anscandi:
    Smod = int(0)
    que = []
    for j in range(N):
        que.append((A[j] % i) *(-1))
        Smod += A[j] % i
    need = Smod//i
    heapq.heapify(que)
    for _ in range(need):
        heapq.heappop(que) * (-1)
    if K>=(sum(que)*(-1)):
        ans = i
        break


print(ans)

Solution (Python):
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort(reverse = True)
    return divisors

import heapq

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

inf = float('inf')
ans = int(0)

N, K = LI()
A = LI()
cou = int(0)
sumA = sum(A)
anscandi = make_divisors(sumA)


for i in anscandi:
    Smod = int(0)
    que = []
    for j in range(N):
        que.append((A[j] % i) *(-1))
        Smod += A[j] % i
    need = Smod//i
    heapq.heapify(que)
    for _ in range(need):
        heapq.heappop(que) * (-1)
    if K>=(sum(que)*(-1)):
        ans = i
        break


print(ans)

Solution (Python):

import sys
from bisect import bisect_left, bisect_right

N,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

s=sum(A)
n=int(s**0.5)


cd=[]

for i in range(1,n+2):
	if s%i==0:
		cd.append(i)
		cd.append(s/i)

cd.sort(reverse=True)

def check(t):
	if t==1: return True
	A_temp=[]
	for x in A:
		A_temp.append(x%t)

	A_temp.sort()

	
	A_temp_sum=[]

	for i,x in enumerate(A_temp):
		if i==0: A_temp_sum.append(x)
		else: A_temp_sum.append(A_temp_sum[-1]+x)

	
	p1=min(bisect_left(A_temp_sum,K),N-2)
	if A_temp_sum[p1]<=K:
		k1=A_temp_sum[p1]
	else:
		
		p1-=1
		k1=A_temp_sum[p1]


	
	k2=t*(N-p1-1)-(A_temp_sum[-1]-A_temp_sum[p1])

	
	if k2<=k1:
		return True
	else:
		return False

for x in cd:
	if check(x):
		print x
		quit()

Solution (Python):
import sys,os,math
from collections import Counter, defaultdict, deque
import bisect
from sys import stdin, stdout
from itertools import repeat
import Queue








def main():
    n, k = map(int, raw_input().split())
    da = map(int, raw_input().split())
    sm = sum(da)
    mx = max(da)
    ans = 1
    for i in range(1, int(math.sqrt(sm))+1):
        if sm%i==0:
            def check(p):
                l = []
                for num in da:
                    l.append(num%p)
                ssm = n*p - sum(l)
                l.sort()
                ps = 0
                uk = 0
                for num in l:
                    uk += num
                    ssm -= p-num
                    if ssm == uk and uk <= k:
                        return True
                return False
            
            if check(i):
                ans = max(ans, i)
            if check(sm/i):
                ans = max(ans, sm/i)

    print ans






main()

Solution (Python):
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string



def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')


input=lambda: sys.stdin.readline().rstrip()
import sys
sys.setrecursionlimit(10**7)

show_flg=False

def devisors(x):
    rt=[1,x]
    for i in range(2,int(x**0.5+1)):
        if x%i==0:
            rt.append(i)
            if x//i!=i:rt.append(x//i)
    return rt
    
n,k=MI()
a=LI()
A=sum(a)
ds=devisors(A)
ans=1

for d in ds:
    r=[j%d for j in a]
    show(r,d)
    if sum(r)%d!=0:
        continue
    r.sort()
    ac=[0]
    for j in range(n):
        ac.append(ac[-1]+r[j])
    for j in range(1,n):
        if ac[j]==(n-j)*d-(ac[n]-ac[j]) and ac[j]<=k:
            ans=max(d,ans)
            show(j,d,ans)

print(ans)
show(ds)

Solution (Python):
n, k = map(int, input().split( ))
a = list(map(int, input().split( )))

a_sum = sum(a)

rt_sm = int(a_sum**(1/2) + 0.01)

fct = []
fct2 = []

for p in range(1,rt_sm+1):
    if a_sum%p == 0:
        fct.append(p)
        fct2.append(a_sum//p)

if fct[-1] == fct2[-1]:
    fct.pop() 

fct += fct2
fct.sort(reverse=True)


for f in fct:
    tmp = []
    for t in a:
        tmp.append(t%f)
    tmp_sum = sum(tmp)
    cnt = 0

    sb = tmp_sum // f
    tmp.sort()
    for i in range(n-sb,n):
        tmp[i] -= f

    if sum(tmp[:n-sb]) <= k:
        ans = f 
        break

print(ans)

Solution (Python):
import math

N, K = map(int, input().split(" "))
As = list(map(int, input().split(" ")))

S = sum(As)
l = []
i = 1
ans = 1

while i*i <= S:
  if S % i == 0:
    l.append(i)
    l.append(S//i)
  i += 1

l.sort(reverse=True)

for i in l:
  ext = 0
  exts = []
  for j in As:
    exts.append(j % i)
    ext += (j % i)
  if ext % i != 0:
    continue
  else:
    p = 0
    exts.sort()
    cnt = ext//i
    for k in range(0, N - cnt):
      p += exts[k]
    if p <= K:
      ans = i
      break

print(ans)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
a_sum=sum(a)
def devisor(x):
    lst=[]
    i=1
    y=x
    while i**2<=y:
        if x%i==0:
            lst.append(i)
            if i!=x//i:
                lst.append(x//i)
        i+=1
    lst.sort()
    return lst
re=devisor(a_sum)
re.sort(reverse=True)
for u in re:
    w=[]
    for v in a:
        w.append(v%u)
    w.sort()
    b=sum(w)
    count=0
    for i in range(b//u):
        w[-i-1]-=u
    for i in w:
        if i>=0:
            count+=i
        else:
            break
    if count<=k:
        print(u)
        break

Solution (Python):
import itertools
import bisect
N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)

    divisors.sort(reverse=True)
    return divisors


divisors = make_divisors(sum(A))
for d in divisors:
    b = []
    for a in A:
        b += [a % d]
    b.sort()
    c = [0] + list(itertools.accumulate(b))
    il = bisect.bisect_left(c, K)
    if il >= len(c) or d * (len(c) - il - 1) - (c[-1] - c[il]) <= K:
        print(d)
        exit()

Solution (Python):
def getlist():
	return list(map(int, input().split()))


N, K = getlist()
A = getlist()
S = sum(A)
n = int((S ** 0.5) // 1) + 1
gcd = []
for i in range(1, n):
	if S % i == 0:
		gcd.append(i)
x = len(gcd)
for i in range(x):
	gcd.append(S // gcd[i])
gcd = sorted(gcd)


ans = 1
for i in gcd:
	P = []
	for j in A:
		x = j % i
		if x != 0:
			P.append([x, i - x])
	P = sorted(P)
	a = 0
	b = 0
	for j in range(len(P)):
		a += P[j][0]
	s = 0
	for j in range(len(P)):
		a -= P[-1 - j][0]
		b += P[-1 - j][1]
		s += P[-1 - j][1]
		if a == b:
			break
	if s <= K:
		ans = i

print(ans)

Solution (Python):
def divisors(N):
    U = int(N ** 0.5) + 1
    L = [i for i in range(1, U) if N % i == 0]
    return L + [N // i for i in reversed(L) if N != i * i]

def main():
    N, K, *A = map(int, open(0).read().split())

    for d in reversed(divisors(sum(A))):
        R = sorted(a % d for a in A)
        if sum(R[:-sum(R) // d]) <= K:
            print(d)
            quit()

if __name__ == '__main__':
    main()

Solution (Python):
def divisors(N):
    U = int(N ** 0.5) + 1
    L = [i for i in range(1, U) if N % i == 0]
    return L + [N // i for i in reversed(L) if N != i * i]

N, K, *A = map(int, open(0).read().split())

for d in reversed(divisors(sum(A))):
    R = sorted(a % d for a in A)
    if sum(R[:-sum(R) // d]) <= K:
        print(d)
        quit()

Solution (Python):
from itertools import accumulate

def divisors(N):
    U = int(N ** 0.5) + 1
    L = [i for i in range(1, U) if N % i == 0]
    return L + [N // i for i in reversed(L) if N != i * i]

N, K, *A = map(int, open(0).read().split())

for d in reversed(divisors(sum(A))):
    R = [0] + list(accumulate(sorted(a % d for a in A)))
    for i in range(N):
        l = R[i]
        r = (N - i) * d - (R[N] - R[i])
        if max(l, r) <= K:
            print(d)
            quit()

Solution (Python):
from heapq import heappop, heappush

n, k = map(int, input().split())
a = list(map(int, input().split()))

s = sum(a)
d = []
for i in range(1, int(s**0.5)+1):
    if s % i == 0:
        heappush(d, -i)
        if i != s // i:
            heappush(d, -(s // i))

while len(d) > 0:
    x = - heappop(d)
    r = list(map(lambda b:b%x, a))
    r.sort()
    L = -1
    for i in range(n):
        if r[i] != 0:
            L = i
            break
    if L == -1:
        if k % x == 0:
            print(x)
            exit()
        else:
            continue
    H = n - 1
    temp = r[L] - (x - r[H])
    count = r[L] + (x - r[H])
    L += 1
    H -= 1
    while L <= H:
        if temp <= 0:
            temp += r[L]
            count += r[L]
            L += 1
        else:
            temp -= x - r[H]
            count += x - r[H]
            H -= 1
        if count > 2 * k:
            break
    
    if count > 2 * k:
        continue
    if temp % (2 * x) == 0:
        print(x)
        exit()

print(1)

Solution (Python):
from heapq import heappush, heappop
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
x = sum(A)
ls = []
for i in range(1, int(x**0.5)+1):
  if x%i==0:
    ls += [i]
    if i != x//i:
      ls += [x//i]
ls.sort(reverse=True)
for i in ls:
  B =[]
  for j in A:
    heappush(B, -1*(j%i))
  y = sum(B)
  while y<0:
    u = heappop(B)
    u += i
    heappush(B,u)
    y += i
  m = 0
  for j in B:
    if j>0:
      m += j
  if m>K:
    continue
  else:
    print(i)
    break

Solution (Python):
from heapq import heappush, heappop
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
x = sum(A)
ls = []
for i in range(1, int(x**0.5)+1):
  if x%i==0:
    ls += [i]
    if i != x//i:
      ls += [x//i]
ls.sort(reverse=True)
for i in ls:
  B =[]
  for j in A:
    heappush(B, -1*(j%i))
  y = sum(B)
  while y<0:
    u = heappop(B)
    u += i
    heappush(B,u)
    y += i
  m = 0
  for j in B:
    if j>0:
      m += j
  if m>K:
    continue
  else:
    print(i)
    break

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



n, k = LI()
L = LI()


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort(reverse=True)
    return divisors


for gcd in make_divisors(sum(L)):
    remainder_l = sorted([i % gcd for i in L if i % gcd])
    remainder_u = [gcd - i for i in remainder_l]
    a = list(accumulate(remainder_l))
    b = list(reversed(list(accumulate(reversed(remainder_u)))))
    if not a:
        print(gcd)
        break
    ng = len(a) - 1
    ok = 0
    while ng > ok + 1:
        mid = (ok + ng) // 2
        if a[mid] > b[mid+1]:
            ng = mid
        elif a[mid] < b[mid+1]:
            ok = mid
        else:
            ok = mid
            break
    if a[ok] <= k:
        print(gcd)
        break

Solution (Python):
from heapq import heappush, heappop


def get_divisors(n):
    divisors = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            heappush(divisors, -i)
            if i != n // i:
                heappush(divisors, -(n // i))

    return divisors


def p_e():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    sum_A = sum(A)

    divisors = get_divisors(sum_A)
    n = len(divisors)
    for _ in range(n):
        h = -heappop(divisors)
        sub_list = []
        for a in A:
            sub_list.append(a % h)

        x = sum(sub_list)
        sub_list.sort()
        
        if sum(sub_list[:-x // h]) <= K:
            print(h)
            exit()

    print(0)


if __name__ == '__main__':
    p_e()

Solution (Python):
import sys
input = sys.stdin.readline

N, K = map(int,input().split())
A = list(map(int, input().split()))
sumA = sum(A)

yakusu = []
for i in range(1,int(sumA**0.5)+1):
    if sumA%i==0:
        yakusu.append(i)
        if sumA//i!=i:
            yakusu.append(sumA//i)
yakusu.sort(reverse=True)

for x in yakusu:
    tmp = list(map(lambda y: y%x, A))
    tmp.sort()
    for i in range(N):
        minuscnt = sum(tmp[:i+1])
        pluscnt = x*(N-i-1)-sum(tmp[i+1:])
        if minuscnt%x == pluscnt%x and K >= max(minuscnt,pluscnt):
            print(x)
            sys.exit()

Solution (Python):
def divisors(n):
    div = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            div.append(i)
            if i**2 != n:
                div.append(n//i)
    div.sort()
    return div

n,k = map(int, input().split())
a = list(map(int, input().split()))

d = divisors(sum(a))
d.reverse()

for x in d:
    b = []
    for i in range(n):
        b.append(x-a[i]%x)
    b.sort()
    c = sum(b)//x
    if sum(b[:-c]) <= k:
        print(x)
        exit()

Solution (Python):
from itertools import accumulate
N, K = map(int, input().split())
A = list(map(int, input().split()))


A_sum = sum(A)
ans_candidates = []
for n in range(1, int(A_sum ** 0.5) + 1):
    if A_sum % n == 0:
        ans_candidates.append(n)
        ans_candidates.append(A_sum // n)
ans_candidates.sort(reverse=True)


for z in ans_candidates:
    need_add = [(z - a % z) % z for a in A]
    need_add.sort()
    need_minus = [z - na for na in need_add]

    need_add = list(accumulate(need_add))
    need_minus = list(accumulate(need_minus))

    for i in range(N):
        if (need_add[i] == need_minus[-1] - need_minus[i]) and (need_add[i] <= K):
            print(z)
            exit()

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))

import math

Asum = sum(A)
candidates = []
for i in range(1, int(math.sqrt(Asum)) + 1):
    if Asum % i == 0:
        candidates.append(i)
        candidates.append(Asum // i)
candidates.sort()




ans = 1
for targ in candidates:
    if targ == 1: continue
    A_tmp = [(Ai % targ) for Ai in A]
    
    A_tmp.sort()
    idx_l = 0
    idx_r = N - 1
    cnt = 0
    while sum(A_tmp) > 0 and idx_l < idx_r:
        cap_l = A_tmp[idx_l]
        cap_r = targ - A_tmp[idx_r]
        A_tmp[idx_l] = (A_tmp[idx_l] - min(cap_l, cap_r)) % targ
        A_tmp[idx_r] = (A_tmp[idx_r] + min(cap_l, cap_r)) % targ
        cnt += min(cap_l, cap_r)
        if A_tmp[idx_l] == 0:
            idx_l += 1
        if A_tmp[idx_r] == 0:
            idx_r -= 1
    
    if cnt <= K and sum(A_tmp) == 0: ans = targ

print(ans)

Solution (Python):
from itertools import accumulate

def getDivisor(x):
    ans = []
    for d in range(1, int(x**0.5)+1):
        if x%d == 0:
            ans.append(d)
            if x//d != d:
                ans.append(x//d)
    return sorted(ans)


N, K = map(int, input().split())
As = list(map(int, input().split()))

sumA = sum(As)
divisors = getDivisor(sumA)

def f(d):
    Bs = [A%d for A in As]
    Bs.sort()
    sumB = sum(Bs)
    accBs = list(accumulate(Bs))
    for i in range(1, N):
        numSub = accBs[i-1]
        numAdd = d*(N-i) - (sumB-accBs[i-1])
        if numSub <= K and numAdd <= K and (numSub-numAdd)%d == 0:
            return True
    return False


for d in reversed(divisors):
    ret = f(d)
    if ret:
        print(d)
        break
else:
    print(1)

Solution (Python):
N , K = map(int, input().split())
A = list(map(int, input().split()))
s = sum(A)
divisor = []
n = 1
while n ** 2 <= s:
    q, r = divmod(s, n)
    if r == 0:
        divisor.append(q)
        divisor.append(n)
    n += 1
ans = 1
for d in divisor:
    m = []
    for a in A:
        m.append(a % d)
    m.sort(reverse=True)
    l = sum(m) // d
    a = l * d - sum(m[:l])
    if a <= K:
        ans = max(ans, d)
print(ans)

Solution (Python):
def divisor(n): 
    ans = [1,n]
    i = 2
    while i**2 <= n:
        if n%i == 0:
            ans.append(i)
            if i**2 < n:
                ans.append(n//i)
        i += 1
    ans.sort()
    return ans

n,k = map(int,input().split())
A = list(map(int,input().split()))
d_max= sum(A)
div = divisor(d_max)[::-1]

for ans in div:
    b = [a%ans for a in A]
    b.sort()
    s = sum(b)
    if sum(b[:-s//ans]) <=k:
        print(ans)
        exit()

Solution (Python):
import math
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    tot = sum(A)

    cand = helper(tot)
    for i in cand:
        rems = []
        for j in range(N):
            r = A[j] % i
            if r > 0:
                rems.append(r)
        rems.sort()

        tot = sum(rems)
        prefixneg = [0]
        for j in range(len(rems)):
            prefixneg.append(prefixneg[-1] + rems[j])

        for j in range(len(rems)+1):
            pos = (len(rems)-j)*i - (tot - prefixneg[j])
            neg = prefixneg[j]
            if max(pos, neg) <= K:
                return i

    return 1


def helper(n):
    div = set()
    for i in range(1, int(math.sqrt(n))+1):
        if n % i == 0:
            div.add(i)
            div.add(n//i)
    return sorted(list(div), reverse = True)

if __name__ == '__main__':
    print(main())

Solution (Python):
import math
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    tot = sum(A)

    cand = helper(tot)
    
    for i in cand:
        rems = []
        for j in range(N):
            r = A[j] % i
            if r > 0:
                rems.append(r)
        rems.sort()
        
        tot = sum(rems)
        prefixneg = [0]
        for j in range(len(rems)):
            prefixneg.append(prefixneg[-1] + rems[j])
        
        for j in range(len(rems)+1):
            pos = (len(rems)-j)*i - (tot - prefixneg[j])
            neg = prefixneg[j]
            
            if max(pos, neg) <= K:
                return i

    return 1


def helper(n):
    div = set()
    for i in range(1, int(math.sqrt(n))+1):
        if n % i == 0:
            div.add(i)
            div.add(n//i)
    return sorted(list(div), reverse = True)

if __name__ == '__main__':
    print(main())

Solution (Python):
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    
    total = sum(A)
    candidate = []
    
    for i in range(1, int(total**0.5)+1):
        if not total % i:
            candidate.extend([i, total//i])
    
    ans = 0
    
    for c in candidate:
        d = sorted([a % c for a in A])
        j = N - sum(d) // c
        
        if sum(d[:j]) <= K:
            ans = max(ans, c)
    
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def solve():
    N,K = map(int , input().split())
    A = list(map(int , input().split()))
    max_total = 22361 
    total = sum(A)
    search_space = set([])

    max_d = 1
    for d in range(1, min(total, max_total) ):
        if total % d == 0:
            search_space.add(d)
            search_space.add(total // d)
    
    
    
    for d in search_space:
        if total % d == 0:
            remainders = [a%d for a in A]
            remainders = sorted(remainders)
            
            positive_rem = 0
            negative_rem = 0
            flag = True
            for rem in remainders:
                if positive_rem+rem <= K:
                    positive_rem += rem
                elif negative_rem+(d-rem) <=K:
                    negative_rem += (d-rem)
                else: 
                    flag = False
                    break

            if flag and d > max_d:
                
                max_d = d
            
    print(max_d)
    
solve()

Solution (Python):

n,k=map(int,input().split())
a=list(map(int,input().split()))
s=sum(a)

res=[]
for d in range(1,int(s**0.5)+1):
	if s%d==0:
		res.append(d)
		res.append(s//d)
ans=[]
for d in res:
	r1=[t%d for t in a]
	r1.sort()
	r2=[(d-t)%d for t in r1]
	r2=r2[::-1]
	x=[r1[0]]
	_=[x.append(x[-1]+r1[i]) for i in range(1,n)]
	y=[r2[0]]
	_=[y.append(y[-1]+r2[i]) for i in range(1,n)]
	
	for i in range(n-1):
		
		if x[i]==y[-i-2] and x[i]<=k:
			ans.append(d)
			break
print(max(ans))

Solution (Python):

n,k=map(int,input().split())
a=list(map(int,input().split()))
s=sum(a)

res=[]
for d in range(1,int(s**0.5)+1):
	if s%d==0:
		res.append(d)
		res.append(s//d)
ans=[]
for d in res:
	r1=[t%d for t in a]
	r1.sort()
	r2=[(d-t)%d for t in r1]
	r2=r2[::-1]
	x=[r1[0]]
	_=[x.append(x[-1]+r1[i]) for i in range(1,n)]
	y=[r2[0]]
	_=[y.append(y[-1]+r2[i]) for i in range(1,n)]
	
	for i in range(n-1):
		
		if x[i]==y[-i-2] and x[i]<=k:
			ans.append(d)
			break
print(max(ans))

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
s = sum(a)
div = []
rdiv = []
for i in range(1,int(s**.5+1)):
  if s % i == 0:
    div.append(i)
    rdiv.append(s//i)
if s == int(s**.5)**2:
  div = di[:-1]
rdiv.reverse()
div += rdiv
div.reverse()

for i in range(len(div)):
  now = div[i]
  tmp = []
  for x in a:
    tmp.append(x%now)
  tmp.sort()
  b = n - sum(tmp) // now
  
  ans = 0
  for j in range(b):
    ans += tmp[j]
  if ans <= k:
    print(now)
    break

Solution (Python):
n, k = list(map(int, input().split()))
nums = list(map(int, input().split()))

def prime_factorize(num):
	x = 1
	fcts = []
	while x * x <= num:
	    if num % x == 0:
	        fcts.append(x)
	        fcts.append(num // x)
	    x += 1
	fcts.sort(reverse = True)
	return fcts

def num_operation(l):
	
	nums_div1 = sorted([x % l for x in nums])
	nums_div2 = [(l - x) % l for x in nums_div1]
	min_op = 10 ** 10

	lf = 0
	rg = sum(nums_div2)
	if rg % l == 0:
		min_op = rg

	for i in range(n):
		lf += nums_div1[i]
		rg -= nums_div2[i]
		if (lf - rg) % l == 0:
			min_op = min(min_op, max(lf, rg))
	return min_op

sum_nums = sum(nums)
fcts = prime_factorize(sum_nums)

for fct in fcts:
	if num_operation(fct) <= k:
		print(fct)
		break

Solution (Python):
n, k = map(int, input().split())
arr = list(map(int, input().split()))




add = sum(arr)

def check(factor):
	mod_factor = [x%factor for x in arr]
	mod_factor.sort(reverse = True)
	p = sum(mod_factor)//factor
	
	
	
	c = sum(factor - mod_factor[i] for i in range(p))
	return c <= k

ans = i = 1
while i*i <= add:
	if add % i == 0:
		if check(i):
			ans = max(ans, i)
		if check(add//i):
			ans = max(ans, add//i)
	i += 1
print(ans)

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def prime_decomposition(n): 
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(i)
    i += 1
  if n > 1:
    table.append(int(n))
  return table

def make_divisors(n): 
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors



n,k = m()

a = l()

p = sum(a)

d = make_divisors(p)

ans = 0

for i in d:
    kkk = [0] * n
    for j in range(n):
        kkk[j] = a[j] % i
    kkk.sort()
    kkkk = sum(kkk)
    nw = 0
    for j in range(n):
        nw += kkk[j]
        nn = kkkk - nw
        if nw <= k and (n-j-1)*i - nn <= k and nw == (n-j-1)*i - nn:
            ans = i
            break
print(ans)

Solution (Python):
n, k = map(int,input().split())
a = list(map(int,input().split()))


def d(x):
    l = []
    for i in range(1, int(x**0.5)+1):
        if x%i==0:
            l.append(i)
            if i != x//i:
                l.append(x//i)
    l.sort(reverse=True)
    return l

s = d(sum(a))
for i in s:
    x = [0 for k in range(n)]
    for j in range(len(a)):
        x[j] = a[j] % i
    x.sort()
    l = [0 for k in range(n+1)]
    r = [0 for k in range(n+1)]
    for j in range(n+1):
        if j == 0:
            l[0] = 0
            r[n-0] = 0
        else:
            l[j] = l[j-1] + x[j-1]
            r[n-j] = r[n-j+1] + (i-x[n-j])
    ans = 1
    for j in range(n+1):
        if l[j] == r[j] and l[j] <= k:
            ans = i
            print(ans)
            exit()

Solution (Python):

"""
Created on Mon Aug 19 22:50:08 2019

@author: Yamazaki Kenichi
"""
import functools

N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()

S = sum(A)

def make_divisors(n):
    divisors = []
    for i in range(1,int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors
def dfs(x,d):
    return x%d

D = make_divisors(S)
ans = 1
for d in D:
    B = list(map(functools.partial(dfs,d=d),A[:]))
    B.sort()
    l, r, tmp = 0, N-1, 0
    res = 0
    while l <= r:
        if B[l] + B[r] + tmp == d:
            res += B[l]
            tmp = 0
            l += 1
            r -= 1
        elif B[l] + B[r] + tmp < d:
            tmp = B[l] + tmp
            res += B[l]
            l += 1
        else:
            tmp = B[r] + tmp - d 

            r -= 1

    if res <= K and d > ans:
        ans = d
print(ans)

Solution (Python):


def slvDivisor(summary):
    divisor = set()
    for d in range(1, int(summary ** 0.5) + 1):
        if summary % d == 0:
            divisor.add(d)
            divisor.add(summary // d)
    return divisor



input1 = list(map(int, input().split()))
n = input1[0]
k = input1[1]
a = list(map(int, input().split()))


sumA = sum(a)
divisor = slvDivisor(sumA)






for d in sorted(divisor, reverse=True):
    modA = [x % d for x in a]
    modA.sort()
    i = sum(modA) // d
    cnt = sum(modA[:-i])
    if cnt <= k:
        print(d)
        break

Solution (Python):
N , K = map(int, input().split())
A = list(map(int, input().split()))
s = sum(A)
divisor = []
n = 1
while n ** 2 <= s:
    q, r = divmod(s, n)
    if r == 0:
        divisor.append(q)
        divisor.append(n)
    n += 1
if n ** 2 == s:
    divisor.pop()
ans = 1
for d in divisor:
    m = []
    for a in A:
        m.append(a % d)
    m.sort(reverse=True)
    l = sum(m) // d
    a = l * d - sum(m[:l])
    if a <= K:
        ans = max(ans, d)
print(ans)