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
import math
from collections import defaultdict


sys.setrecursionlimit(10**7)


mod = 10**9 + 7


def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))



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

N,K = II()
A = III()

sumA = sum(A)
kouho = make_divisors(sumA)

for i in kouho:
  if sumA%i != 0:
    continue
  r = [0]*N
  for j in range(N):
    r[j] = A[j]%i
  sumr = sum(r)
  if sumr%i != 0:
    continue
  r.sort(reverse=True)
  num = 0
  for k in range(sumr//i):
    num += i-r[k]
  if num <= K:
    print(i)
    exit()

Solution (Python):


def factors(N): 
    from collections import deque
    ret = deque()
    middle = int( N**(1/2))
    for i in range(1, middle):
        if N%i == 0:
            ret.append(i)
            ret.append(N//i)
            
    if N%middle == 0:
        ret.append(middle)
        if middle != N//middle:
            ret.append(N//middle)
    return ret

def main():
    N, K = map( int, input().split())
    A = list( map( int, input().split()))
    sa = sum(A)
    R = factors(sa)
    l = len(R)
    E = [0]*l
    ans = 0
    for r in R:
        cnt = 0
        E = list( map( lambda x:x%r, A))
        E.sort()
        s = sum(E)
        now = 0
        if s - E[-1] <= K:
            if ans < r:
                ans = r
            continue
        if s == 0:
            continue
        v = 0
        w = N-1
        cnt = 0
        while w-v != 0:
            if E[v] == 0:
                v += 1
                continue
            if E[w] == 0:
                w -= 1
                continue
            if E[v] + E[w] >= r:
                E[v] -= r - E[w]
                cnt += r - E[w]
                E[w] = 0
                w -= 1
            else:
                E[w] += E[v]
                cnt += E[v]
                E[v] = 0
                v += 1
        if cnt <= K:
            if r > ans:
                ans = r
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
N,K=map(int,input().split())
AL=list(map(int,input().split()))
summ=0
for i in range(N):
  summ+=AL[i]
candidate=set()
for i in range(1,int(summ**0.5)+1):
  if summ%i==0:
    candidate.add(i)
    candidate.add(summ/i)
ans=0
for x in candidate:
  need=0
  r=[0]*N
  for i in range(N):
    r[i]=AL[i]%x
  r.sort()
  rsum=0
  for i in range(N):
    rsum=rsum+r[i]
  rindex=int(rsum/x)
  lindex=N-int(rsum/x)
  for i in range(lindex):
    need+=r[i]
  if need<=K:
    ans=max(ans,x)
print(int(ans))

Solution (Python):
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
candidate=set()
result=0
x=1
while x**2<=(sum(a)+1):
	if sum(a)%x==0:
		candidate.add(x)
		candidate.add(int(sum(a)/x))
	x+=1

for i in candidate:
	b=[0]*len(a)
	for j in range(len(a)):
		b[j]=int(a[j]%i)
	b.sort()
	if sum(b[:int(len(b)-sum(b)/i)])<=k:
		result=max(result,i)

	
	
	
print(result)

Solution (Python):
import math
def divisors(n, sort=True, reverse=False):
    sqn = math.sqrt(n)
    sqn_int = int(sqn)
    div_list = []
    if sqn == sqn_int:
        div_list.append(sqn_int)
        loop = range(1,sqn_int)
    else:
        loop = range(1,sqn_int + 1)
    for i in loop:
        if n % i == 0:
            div_list.append(i)
            div_list.append(n//i)
    if sort:
        div_list.sort(reverse=reverse)
    return div_list

inpl = lambda: list(map(int,input().split()))
N, K = inpl()
A = inpl()
S = sum(A)
div = divisors(S, reverse=True)
for d in div:
    mods = [ a % d for a in A ]
    mods.sort()
    Sm = sum(mods)
    h = Sm // d
    minK = min(d*(h+1) - sum(mods[N-h-1:]), sum(mods[:N-h]))
    if minK <= K:
        print(d)
        break

Solution (Python):
from itertools import accumulate

def divisor(n):
    ass = []
    for i in range(1,int(n**0.5)+1):
        if n%i == 0:
            ass.append(i)
            if i**2 == n:
                continue
            ass.append(n//i)
    return sorted(ass, reverse=True)

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

for x in div:
    r = sorted([i%x for i in a])
    sr = sum(r)
    if sr == 0:
        print(x)
        exit()
    cr = list(accumulate(r))
    for i, j in enumerate(cr):
        if j == x*(n-i-1) - (sr-j) and j <= k:
            print(x)
            exit()

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

N, K = [int(item) for item in input().split()]
A = [int(item) for item 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)

    
    return divisors


d_cand = make_divisors(sum(A))
d_cand.sort()
d_cand.reverse()

for d in d_cand:
    r = [a % d for a in A]
    rp_sum = sum(r)
    rm_sum = 0
    if rp_sum % d != 0:
        continue
    r.sort()

    for i in range(N):
        
        rp_sum -= r[i]
        rm_sum += r[i]

        n_min = max(0, -(rm_sum + rp_sum) / d + (N - i - 1))
        n_max = (K - rm_sum) / d
        if n_min <= n_max and (n_min % 1 == 0 or n_max % 1 == 0 or int(n_min) != int(n_max)):
            print(d)
            break
    else:
        continue
    break

Solution (Python):
import numpy as np

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

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

arr = np.array(a)
wa = sum(arr)
divisors = make_divisors(wa)

ans = 1
for div in divisors:
    lst = [0] * n
    for i in range(n):
        lst[i] = a[i] % div
    lst.sort()
    lst = [i for i in lst if i != 0]
    if lst == []:
        ans = max(ans, div)
    arr = np.array(lst)
    arr_amari = np.cumsum(arr)
    arr_reversed = arr[::-1]
    arr_husoku = np.cumsum(div - arr_reversed)[::-1]
    for i in range(len(arr_amari)-1):
        if arr_amari[i] == arr_husoku[i+1] and arr_amari[i] <= k:
            ans = max(ans, div)

print(ans)

Solution (Python):
def e_max_gcd(N, K, A):
    def divisor_list(n):
        """n の正の約数のリスト"""
        ret = []
        for k in range(1, int(n**0.5) + 1):
            if n % k == 0:
                ret.append(k)
                if k != n // k:
                    ret.append(n // k)
        ret.sort()
        return ret

    ans = 1  
    for c in divisor_list(sum(A)):  
        r = sorted([a % c for a in A])
        
        
        
        
        
        
        
        
        
        
        if sum(r[:N - sum(r) // c]) <= K:
            ans = max(ans, c)
    return ans

N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
print(e_max_gcd(N, K, A))

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

    S = sum(A)
    divisors = make_divisors(S)
    
    for d in divisors:
        R = list(map(lambda x: x % d, A))
        R = [r for r in R if r != 0]
        R.sort()
        cnt_m, cnt_p = 0, 0
        for r in R:
            if cnt_m <= K - r:
                cnt_m += r
            else:
                cnt_p += d - r
        if cnt_m <= K and cnt_p <= K:
            print(d)
            exit()

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

if __name__ == "__main__":
    main()

Solution (Python):
N, K = [int(zz) for zz in input().split()]
A = [int(zz) for zz in input().split()]

A.sort(reverse=True)

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

P = make_divisors(sum(A))
P.sort(reverse=True)

for p in P:
    z = 0
    for i in range(N):
        z += A[i] % p
    if z % p == 0:
        B = [i % p for i in A]
        B.sort(reverse=True)
        q = z//p
        j = 0
        for i in range(q):
            j += p-B[i]
        if K >= j:
            print(p)
            exit()
            
print(p)

Solution (Python):
N, K = map(int, input().split())
A = [int(i) for i 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


lst = make_divisors(sum(A))

for l in lst:
    cnt_m, cnt_p = 0, 0
    ml = sorted([i % l for i in A])
    for m in ml:
        if cnt_m <= K-m:
            cnt_m += m
        else:
            cnt_p += l-m
    if cnt_m <= K and cnt_p <= K:
        print(l)
        exit()
print(1)

Solution (Python):
from collections import defaultdict,deque
import numpy as np
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N,K = inpl()
AA = np.array(inpl())
S = sum(AA)

koho = set()
for i in range(1,int(math.sqrt(S))+3):
    if S%i == 0:
        koho.add(i)
        koho.add(S//i)

koho = sorted(list(koho))
ans = 0
for k in koho:
    AA_modk = sorted(AA%k)
    

    Ln = Rn = 0
    L = 0
    R = N-1
    while L <= R:
        if Ln <= Rn:
            Ln += AA_modk[L]
            L += 1
        else:
            Rn += k - AA_modk[R]
            R -= 1

    

    if K >= Ln:
        ans = max(k,ans)

print(ans)

Solution (Python):
import math

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

candidates = []
sumA = sum(A)

i = 1
while i <= math.sqrt(sumA):
    if sumA % i == 0:
        candidates.append(i)
        candidates.append(int(sumA / i))
    i += 1
    
candidates = sorted(candidates, reverse = True)
end = 0

for i in range(len(candidates)):
    if end:
        break
    
    T = [0] * len(A)
    for j in range(len(A)):
        T[j] = A[j] % candidates[i]
    T = sorted(T)
    left = sum(T)
    right = 0
    need = max(left, right)
    for l in range(len(T) - 1, -1, -1):
        left -= T[l]
        right += candidates[i] - T[l]
        need = min(need, max(left, right))
    if need <= K:
        print(candidates[i])
        end = 1

Solution (Python):
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
div = []
A_sum = sum(A)
i = 1
while i ** 2 < A_sum:
    if A_sum % i == 0:
        div.append(i)
        div.append(A_sum // i)
    i += 1

div.sort(reverse=True)

for j in div:
    mod = [a % j for a in A]
    mod.sort()
    s = sum(mod)
    if sum(mod[:-s // j]) <= K:
        print(j)
        break

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

s = sum(a)
dl = []
k = 1
while k ** 2 <= s:
  if s % k == 0:
    dl.append(k)
    dl.append(s // k)
  k += 1
  
cand = []
for d in dl:
  op = 0
  b = [n % d for n in a]
  b.sort()
  b.reverse()
  summod = sum(b)
  for i in range(summod // d):
    op += d - b[i]
  if op <= K:
    cand.append(d)
    
print(max(cand))

Solution (Python):
from itertools import accumulate

def divisors(n):
    divs = []
    for i in range(1, int(n ** 0.5) + 1):
        d, m = divmod(n, i)
        if m == 0:
            divs.append(i)
            divs.append(d)
    divs.sort(reverse=True)
    return divs

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

for div in divs:
    m = [ x % div for x in a ]
    m.sort()
    l = [0] + list(accumulate(m))
    r = list(reversed([0] + list(accumulate(reversed([div - x for x in m])))))
    for i in range(len(l)):
        if l[i] == r[i]:
            if l[i] <= k:
                print(div)
                exit()
            break
        elif l[i] > r[i]:
            break

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)
    return sorted(divisors, reverse=True)


if __name__ == '__main__':
    n, k = map(int, input().split(' '))
    A = list(map(int, input().split(' ')))
    divisors = make_divisors(sum(A))
    for divisor in divisors:
        remainders = sorted(map(lambda x: x % divisor, A))
        idx = n - sum(remainders) // divisor
        ope_num = sum(remainders[:idx])
        if ope_num <= k:
            print(divisor)
            break

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
sun = sum(a)
cd = []
rcd = []
for i in range(1,int(sun**0.5)+1):
  if sun % i == 0:
    cd.append(i)
    rcd.append(sun//i)
rcd.reverse()
if cd[-1] == rcd[0]:
  del cd[-1]
gcd = cd + rcd
gcd.reverse()
count = 0
while True:
  g = gcd[count]
  b = list(map(lambda x: x%g, a))
  moon = sum(b) // g
  b.sort()
  luna = sum(b[:n-moon])
  if luna <= k:
    print(g)
    break
  count += 1

Solution (Python):
def yakusu(n):
    m = 1
    re = []
    while m ** 2 < n:
        a, md = divmod(n, m)
        if md == 0:
            re.append(m)
            re.append(a)
        m += 1
    if m ** 2 == n:
        re.append(m)
    return re

n, k = map(int, input().split())
aa = list(map(int, input().split()))
yk = yakusu(sum(aa))
yk.sort(reverse=True)
for y in yk:
    rs = [a % y for a in aa if a % y]
    if not rs:
        print(y)
        exit()
    if len(rs)==1:continue
    rs.sort()
    l = 0
    sl = rs[0]
    r = len(rs) - 1
    sr = y - rs[r]
    while 1:
        if l + 1 == r:
            if sl == sr and sl <= k:
                print(y)
                exit()
            else:
                break
        if sl > sr:
            r -= 1
            sr += y - rs[r]
            if sr > k: break
        else:
            l += 1
            sl += rs[l]
            if sl > k: break
print(1)

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

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

S = sum(A)
L = []
for s in range(1, S+1):
    if s > math.sqrt(S) + 1:
        break
    if S % s == 0:
        L.append(S//s)
        L.append(s)
L.sort(reverse = True)

for l in L:
    T = [a%l for a in A]
    T.sort()
    ans = False
    R = []
    r = 0
    for t in T:
        r += t
        R.append(r)
    
    for i in range(N):
        
        if R[i] > K:
            break
        if R[N-1] == l*(N-i-1):
            ans = True
            break
        
    if ans:
        print(l)
        break

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

As = sorted(As)
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

divs = sorted(make_divisors(sum(As)),reverse=True)

from itertools import accumulate
for div in divs:
    
    
    
    bs_plus = sorted([a%div for a in As])
    bs_minus = [(div-a)%div for a in bs_plus]
    acc_bs_p = list(accumulate(bs_plus))
    acc_bs_m = list(accumulate(bs_minus))
    for i in range(N):
        plus = acc_bs_p[i]
        minus = acc_bs_m[N-1]-acc_bs_m[i]
        if plus == minus and plus <= K:
            
            print(div)
            exit(0)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
S = sum(A)
D = []
i = 1
while i * i <= S:
    if S % i == 0:
        D.append(i)
        if S // i != i:
            D.append(S // i)
    i += 1

D = sorted(D, reverse=True)

for d in D:
    mod = [a % d for a in A]
    mod = sorted(mod)
    s = sum(mod)
    if sum(mod[:-s//d]) <= K:
        print(d)
        exit()

Solution (Python):
def divisible(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

N, K = map(int,input().split())
xs = list(map(int,input().split()))
divs = divisible(sum(xs))

for div in divs :
    test = [x % div for x in xs]
    test.sort()
    sum_test = sum(test)
    change = sum(test[:-sum_test//div])
    if change <= K :
        ans = div
        break
print(ans)

Solution (Python):
import decimal,math
decimal.getcontext().prec = 100
def sum1(pre,l,r):
    l=max(l,0)
    if(l==0):
        return pre[r]
    return pre[r] - pre[l - 1]
abc=[]
def printDivisors(n) : 
    i = 1
    while i <= math.sqrt(n): 
          
        if (n % i == 0) : 
              
            if (n / i == i) : 
                abc.append(i) 
            else : 
                abc.append(i)
                abc.append(n/i)
        i = i + 1
n,k=map(int,raw_input().split())

arr=map(int,raw_input().split())
s=sum(arr)
printDivisors(s)
abc.sort()
abc=abc[::-1]
ans=1
for i in abc:
    l=0
    flag=0
    c=[]
    for j in arr:
        c.append(j%i)
    c.sort()
    if(sum(c[:-sum(c)/i])<=k):
        print i
        exit()

Solution (Python):
import sys


def main():
  n, k = map(int, input().split())
  alist = list(map(int, input().split()))
  suma = sum(alist)
  i = 1
  yaku = []
  while i*i<=suma:
    if suma % i == 0:
      yaku.append(i)
      if suma//i != i:
        yaku.append(suma//i)
    i += 1

  for d in sorted(yaku, key=lambda x: -x):
    rem = [a%d for a in alist]
    rem = list(sorted(rem))
    sumr = d*len(rem) - sum(rem)
    kc = 0
    for _r in rem:
      kc += _r
      sumr = sumr - (d-_r)
      if max(kc, sumr) <= k:
        print(d)
        sys.exit(0)


if __name__ == '__main__':
  main()

Solution (Python):
n,k,*a=map(int,open(0).read().split())
s=sum(a)
b=[]
for i in range(1,30000):b+=[s//i,i]*(s%i<1)
m=1
for i in b:c=sorted(j%i for j in a);m=max(m,i*(sum(c[:-sum(c)//i])<=k))
print(m)

Solution (Python):
n,k,*l=map(int,open(0).read().split())
s=sum(l)
L=[]
for i in range(1,30000):L+=[i,s//i]*(s%i<1)
t=1
for d in L:a=sorted([m%d for m in l]);t=max(t,d*(sum(a[:-sum(a)//d])<=k))
print(t)

Solution (Python):
n,k,*l=map(int,open(0).read().split())
s=sum(l)
L=[]
for i in range(1,30000):L+=[i,s//i]*(s%i<1)
L.sort()
for d in L[::-1]:
 a=sorted([m%d for m in l])
 if sum(a[:n-sum(a)//d])<=k:print(d);exit()

Solution (Python):
n,k,*a=map(int,open(0).read().split())
s=sum(a)
b=[]
for i in range(1,int(s**.5)+1):b+=[s//i,i]*(s%i<1)
m=1
for i in b:c=sorted(j%i for j in a);m=max(m,i*(sum(c[:-sum(c)//i])<=k))
print(m)

Solution (Python):
n,k,*a=map(int,open(0).read().split())
s,m=sum(a),1
for i in sum([[s//i,i]for i in range(1,int(s**.5)+1)if s%i<1],[]):
  p=[0]+sorted(j%i for j in a)
  for j in range(n):p[j+1]+=p[j]
  f=t=0
  for j in range(n):
    f|=t==p[~j]<=k
    t+=i-p[~j]+p[-j-2]
  m=max(m,i*f)
print(m)

Solution (Python):
n,k,*a=map(int,open(0).read().split())
s,m=sum(a),1
for i in sum([[s//i,i]for i in range(1,int(s**.5)+1)if s%i<1],[]):
  p=[0]+sorted(j%i for j in a)
  for j in range(n):p[j+1]+=p[j]
  f=t=0
  for j in range(n):
    f|=t==p[~j]<=k
    t+=i-p[~j]+p[-j-2]
  m=max(m,i*f)
print(m)

Solution (Python):
n,k,*l=map(int,open(0).read().split());s=sum(l);L=[]
for i in range(1,int(s**0.5)+1):
  if s%i==0:L+=[i,s//i]
for d in sorted(L)[::-1]:
  a=sorted([m%d for m in l])
  if sum(a[:n-sum(a)//d])<=k:print(d);exit()

Solution (Python):
import sys
import math

def get_divisor(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

def main():
    _, K = map(int, input().split())
    *A, = map(int, input().split())

    aSum = sum(A)
    divs = get_divisor(aSum)

    for d in divs:
        r = [a % d for a in A]
        r.sort()

        sumRm = d*len(r) - sum(r)

        tmpSum = 0
        for _r in r:
            tmpSum += _r
            sumRm = sumRm - (d-_r)
            op = max(tmpSum, sumRm)
            if op <= K:
                print(d)
                sys.exit()

if __name__ == '__main__':
    main()

Solution (Python):
import math
def dprint(*args):
  pass


N, K = [int(x) for x in input().split()]
As = [int(x) for x in input().split()]
S = sum(As)
Ds = []
for i in range(1, 1+int(math.sqrt(S))):
  if S % i == 0:
    Ds.append(i)
    Ds.append(S//i)
Ds = sorted(set(Ds))

for d in Ds[::-1]:
  if d == 1:
    print(1)
    exit()
  dprint(d)
  rs = []  
  for a in As:
    rs.append(a % d)
  rs = sorted(rs)
  dprint(rs)
  total = sum(rs)
  n = total//d
  num = 0
  for i in range(N - n, N):
    rs[i] -= d
    num -= rs[i]
  dprint(rs)
  dprint(num)
  if num <= K:
    print(d)
    exit()

Solution (Python):
n,k,*l=map(int,open(0).read().split())
s=sum(l);L=[]
for i in range(1,int(s**0.5)+1):
	if s%i==0:L+=[i,s//i]
L.sort()
for d in L[::-1]:
	a=sorted([m%d for m in l])
	if sum(a[:n-sum(a)//d])<=k:print(d);exit()

Solution (Python):
n,k,*l=map(int,open(0).read().split())
def f(n):
	d=[]
	for i in range(1,int(n**0.5)+1):
		if n%i== 0:
			d+=[i]
			if i!=n//i:
				d+=[n//i]
	d.sort()
	return d[::-1]
for d in f(sum(l)):
	a=sorted([m%d for m in l])
	if sum(a[:n-sum(a)//d])<=k:
		print(d);exit()

Solution (Python):
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
A.sort()

s = sum(A)
i = 1
q1 = []
q2 = []
while i * i <= s :
    if s % i == 0 :
        q1.append(i)
        if i * i != s :
            q2.append(s//i)
    i += 1
prime = q2 + q1[::-1]

def solve() :
    for p in prime :
        m = [a%p for a in A]
        m.sort()
        
        dpl = [0] * N
        dpr = [0] * N
        
        dpl[N-1] = p - m[N-1]
        dpr[0] = m[0]
        for i in range(N-2, -1, -1) :
            dpl[i] = dpl[i+1] + p - m[i]
        for i in range(1, N) :
            dpr[i] = dpr[i-1] + m[i]
        n = float('inf')
        for i in range(1, N) :
            n = min(n, max(dpl[i], dpr[i-1]))
        if n <= K :
            return p
            
print(solve())

Solution (Python):
from fractions import gcd
from copy import deepcopy as dc
N, K = map(int, input().split())
A = list(map(int, input().split()))
def 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
div = divisors(sum(A))
def check(g):
  Atemp = dc(A)
  for i in range(N):
    Atemp[i] %= g
  Atemp.sort()
  i = len(A) - sum(Atemp) // g
  
  if max(sum(Atemp[: i]), len(Atemp[i: ]) * g - sum(Atemp[i: ])) <= K:
    return True
  else:
    return False

res = 1
for d in div:
  if check(d) == True:
    res = d
print(res)

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

S = sum(A)

def divisor(n):
    res = []
    rn = int(n**0.5) + 1 
    for i in range(1, rn):
        if n % i == 0:
            res.append(i)
            if i != n / i:
                res.append(int(n / i))
    return sorted(res)

Y = divisor(S)

ans = 1
for i in range(len(Y)-1, 0, -1):
    cnt = []
    for j in range(N):
        cnt.append(A[j] % Y[i])

    cnt = sorted(cnt)

    cnt_s = [0]
    tmp = 0
    for j in range(N):
        tmp += cnt[j]
        cnt_s.append(tmp)
    minc = 10**10
    for j in range(N+1):
        M = cnt_s[j]
        P = (N-j)*Y[i] - (cnt_s[N]-cnt_s[j])
        minc = min(max(M,P),minc)
    if minc <= K:
        ans = Y[i]
        break

print(ans)

Solution (Python):
def yakusu(n):
    results = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            results.append(i)
            results.append(n // i)
    return results

N, K = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for i in yakusu(sum(A)):
    M = sorted(map(lambda x: [x % i, (i-x % i)%i], A), key=lambda x: x[0])
    s = sum(map(lambda x: x[1], M))
    sm = 0
    for j, (m, m2) in enumerate(M):
        sm += m
        s -= m2
        if sm == s and s <=K:
            ans = max(ans, i)
print(ans)

Solution (Python):
import sys
import math
MAX_INT = int(10e9)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def judge(x):
  l = []
  for i in range(N):
    l.append(a[i]%x)
  l.sort()

  aa = 0
  bb = 0
  for i in l:
    tmp = i
    if aa + tmp > K:
      bb += x-tmp
      if bb > K:
        return False
    else:
      aa += tmp
  else:
    return True

N,K = IL()
a = IL()
num = sum(a)
ans = []

for i in range(1,int(math.sqrt(num)+1)):
  if num%i == 0:
    if judge(i):
      ans.append(i)
    if judge(num//i):
      ans.append(num//i)

print(max(ans))

Solution (Python):
inf = float('inf')

from math import floor


def divisor(n):
    left = [1]
    right = [n]
    sup = floor(pow(n,1/2))
    for p in range(2,sup+1):
        if n % p == 0:
            if p == n//p:
                left.append(p)
                continue
            left.append(p)
            right.append(n//p)
    res = left + right[::-1]
    return res

def accmulate(array):
    global cs
    cs = [0]*(len(array)+1)
    for i in range(len(array)):
        cs[i+1] = cs[i] + array[i]

def query(l,r):
    return cs[r] - cs[l]

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

S = sum(A)
for d in divisor(S)[::-1]:
    nA = sorted([a % d for a in A])
    accmulate(nA)
    cnt = inf
    for i in range(N+1):
        minus = -query(0,i)
        plus = d*(N-i) - query(i,N)
        if minus + plus == 0:
            cnt = min(cnt, plus)
    if cnt <= K:
        print(d)
        exit()

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

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

for d in make_divisors(sum(A)):
  if d==1:
    print(d)
    exit()
  A_ = [a%d for a in A if a%d]
  if max(A_)+K < d:
    continue  
  A_.sort()
  lSum, rSum = 0, 0
  K_need = 1e9
  for a in A_:
    rSum += d - a
  for a in A_:
    lSum += a
    rSum -= d - a
    K_need = min(K_need, max(lSum,rSum))
  if K_need <= K:
    print(d)
    exit()

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)
    return divisors


def main():
    n, k = map(int, input().split())
    *a, = map(int, input().split())

    asum = sum(a)
    divs = make_divisors(asum)
    ans = 1
    for d in divs:
        r = sorted([aa % d for aa in a])
        j = n - 1 - sum(r) // d
        if sum(r[:j+1]) <= k:
            ans = max(ans, d)
    print(ans)


main()

Solution (Python):
N, K = map(int, input().split(' '))
A = list(map(int, 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

sum_A = sum(A)
d_candidate = make_divisors(sum_A)

for d in d_candidate:
    R = [a % d for a in A]
    R.sort()
    sum_R = sum(R)

    X, mod = divmod(sum_R, d)
    if mod != 0:
        continue

    if sum(R[:N-X]) <= K and (X * d - sum(R[-X:])) <= K:
        print(d)
        break

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
S = sum(A)
ans = 1
C = []
for i in range(1,int(S**0.5+1)):
    if S%i == 0:
        C.append(i)
        C.append(S//i)
for c in C:
    B = []
    for i in A:
        B.append(i%c)
    B.sort()
    hoge = 0
    sum_b = sum(B)
    for i in range(N):
        hoge += B[i]
        sum_b -= B[i]
        if hoge <= K and hoge == c*(N-i-1)-sum_b:
            ans = max(ans,c)
print(ans)

Solution (Python):
from collections import deque

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



total = sum(A)
divisor = []
divisor2 = []
for i in range(1, int(total ** 0.5) + 1):
    if total % i == 0:
        divisor.append(i)
        divisor2.append(total // i)
    if total ** 0.5 == int(total ** 0.5):
        divisor.append(int(total ** 0.5))
divisor2.reverse()
divisor += divisor2

while divisor:
    d = divisor.pop()
    remainder = []
    for i in range(N):
        if A[i] % d != 0:
            remainder.append(A[i] % d)
    remainder.sort()
    remainder = deque(remainder)

    times = 0
    while remainder:
        pre = remainder.popleft()
        times += pre
        remainder[-1] += pre

        while remainder != deque([]) and remainder[-1] >= d:
            post = remainder.pop()
            if remainder != deque([]):
                remainder[-1] += post - d
            else:
                continue
    
    if times <= K:
        print(d)
        exit()

Solution (Python):




def get_fact(N):
  ret = set([N])
  for i in range(2,int(N**0.5)+2):
    if N%i == 0:
      ret.add(i)
      ret.add(N//i)
  return ret


def main():
  
  N,K = map(int, input().split())
  An = list(map(int, input().split()))

  
  fset = get_fact(sum(An))
  ans = 1
  for f in fset:
    Amod = [a%f for a in An]
    Amod = sorted(Amod)
    for i in range(len(Amod)):
      if sum(Amod[:i])==f*(N-i)-sum(Amod[i:]) and sum(Amod[:i])<=K:
        ans = max(ans,f)
  return ans


if __name__ == "__main__":
  ans = main()
  print(ans)