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



Sample input:
2 3
8 20


Sample output:
7

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
S=sum(A)
A.sort(reverse=True)

L=[]
l=[]

for i in range(2,10**5):
    if S%i==0:
        L.append(i)
        l.append(0)
        while S%i==0:
            l[-1]+=1
            S//=i
if S !=1:
    L.append(i)
    l.append(1)
#print(L)
#print(l)

C=[1]
for i in range(len(L)):
    B=[]
    for j in C:
        for k in range(l[i]+1):
            B.append(j*(L[i]**k))
    C=B
B.sort(reverse=True)
#print(B)

#print(A)

for i in range(len(B)):
    D=[]
    for j in range(len(A)):
        D.append(A[j]%B[i])
    D.sort(reverse=True)
    #print(D)
    E=sum(D)
    p=E
    n=0
    for j in range(len(A)):
        n+=-D[j]+B[i]
        p-=D[j]
        if n==p:
            break
    if p<=K:
        print(B[i])
        #print(K,p,n)
        exit()

Submission status: Wrong Answer

Solution code:
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)):
    ret = 0
    for i in L:
        ret += min(i % gcd, gcd - i % gcd)
    if ret // 2 <= k:
        print(gcd)
        break

Submission status: Wrong Answer

Solution code:
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)):
    ret = 0
    for i in L:
        if -gcd < i < gcd:
            ret += min(gcd - i, i - (-gcd))
        else:
            ret += min(i % gcd, gcd - i % gcd)
    if ret // 2 <= k:
        print(gcd)
        break

Submission status: Wrong Answer

Solution code:
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 = 7*(N-i-1)-sum(tmp[i+1:])
        
        if minuscnt%x == pluscnt%x and K >= max(minuscnt,pluscnt):
            print(x)
            sys.exit()

Submission status: Wrong Answer

Solution code:
from math import ceil, floor


def p_d():
    S = input()

    st = 0
    ans = []

    while st < len(S):

        rc = 0
        while st < len(S) and S[st] == "R":
            rc += 1
            st += 1

        lc = 0
        while st < len(S) and S[st] == "L":
            lc += 1
            st += 1

        l = ceil(rc / 2) + lc // 2
        r = rc // 2 + ceil(lc / 2)

        ans.extend([0] * (rc - 1))
        ans.append(l)
        ans.append(r)
        ans.extend([0] * (lc - 1))

    print(*ans)


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)

        if x <= K * 2 and x % h == 0:
            print(h)
            exit()

    print(0)


if __name__ == '__main__':
    p_e()

Submission status: Wrong Answer

Solution code:
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)

        if x <= K * 2:
            print(h)
            exit()

    print(0)


if __name__ == '__main__':
    p_e()

Submission status: Wrong Answer

Solution code:
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)

        if sum(sub_list) <= K * 2 and sum(sub_list) % 2 == 0:
            print(h)
            exit()

    print(0)


if __name__ == '__main__':
    p_e()

Submission status: Wrong Answer

Solution code:
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()

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())

A = list(map(int, input().split()))
sum_A = sum(A)

for i in range(1,sum_A+1):
    if sum_A % i != 0:
        continue
    gcd = sum_A // i
    modulos = sorted(list((a % gcd if a%gcd <= gcd//2 else a % gcd - gcd) for a in A))
    for i in range(sum(modulos) // gcd):
        modulos[i] += gcd
    if sum(map(abs,modulos)) // 2 <= K:
        break

print(gcd)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())

A = list(map(int, input().split()))
sum_A = sum(A)

for i in range(1,sum_A+1):
    if sum_A % i != 0:
        continue
    gcd = sum_A // i
    modulos = sorted([min(a % gcd, gcd - a % gcd) for a in A])
    if sum(modulos[:N-1]) <= K:
        break

print(gcd)

Submission status: Wrong Answer

Solution code:
def solve():
    N,K = map(int , input().split())
    A = list(map(int , input().split()))
    max_A = max(A)
    total = sum(A)
    # ave_region = total//N

    max_d = 1
    for d in A:
        if all(a%d==0 for a in A) and d > max_d:
            max_d = d
    
    search_region = range(max_d, max_A+K)
    
    for d in search_region:
        if total % d == 0:
            remainders = [a%d for a in A]
            remainders = sorted(remainders)
            # print(d, 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 :
                # print(d, remainders, positive_rem, negative_rem)
                max_d = d
            
    print(max_d)
    
solve()

Submission status: Wrong Answer

Solution code:
import math

def solve():
    N,K = map(int , input().split())
    A = list(map(int , input().split()))
    total = sum(A)
    max_region = 22360679
    search_region = min( total+1, max_region+1 )
    
    max_d = 1
    for d in range(2, search_region):
        if total % d == 0:
            remainders = [a%d for a in A]
            remainders = sorted(remainders)
            # print(d, 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 :
                # print(d, remainders, positive_rem, negative_rem)
                max_d = d
            
    print(max_d)
    
solve()

Submission status: Wrong Answer

Solution code:
#56 ABC136E
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)]
	#print(r1,r2,d,x,y)
	for i in range(n-1):
		#print(x[i],y[-i-2])
		if x[i]==y[-i-2] and x[i]<k:
			ans.append(d)
			break
print(max(ans))

Submission status: Wrong Answer

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

# n, k = 4, 5
# arr = list(map(int, "10 1 2 22".split()))

add = sum(arr)

def check(factor):
	mod_factor = [x%factor for x in arr]
	left, right = 0, n-1
	reqd, rem = 0, 0
	mod_factor.sort()
	counter = 0
	while left <= right:
		reqd = factor - mod_factor[right]
		while mod_factor[right]% factor != 0:
			if reqd < mod_factor[left]:
				mod_factor[left] -= reqd
				mod_factor[right] = 0
				counter += reqd
			else:
				mod_factor[right] -= mod_factor[left]
				reqd -= mod_factor[left]
				counter += mod_factor[left]
				left += 1
		right -= 1
	return counter <= 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)

Submission status: Wrong Answer

Solution code:
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]
	left, right = 0, n-1
	reqd = rem = 0
	while left <= right:
		if rem <= 0:
			rem += mod_factor[left]
			reqd += mod_factor[left]
			left += 1
		else:
			rem -= factor - mod_factor[right]
			reqd += factor - mod_factor[right]
			right -= 1
	if reqd > 2*k: return 0
	if rem != 0: return 0
	return 1

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)

Submission status: Wrong Answer

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

add = sum(arr)

factors = []
i = 1
while i*i <= add:
	if add % i == 0:
		factors.append(i)
		if i*i != add:
			factors.append(add // i)
	i += 1

factors.sort(reverse = True)
ans = None
for factor in factors:
	mod_arr = [x%factor for x in arr]
	mod_arr.sort()
	start_ptr, end_ptr = 0, n-1
	counter = 0
	while  start_ptr < end_ptr:
		while mod_arr[end_ptr]%factor != 0:
			mod_arr[end_ptr] += mod_arr[start_ptr]
			counter += mod_arr[start_ptr]
			mod_arr[start_ptr] = 0
			start_ptr += 1
		end_ptr -= 1
	if counter <= k:
		ans = factor
		break
print(ans)

Submission status: Wrong Answer

Solution code:
l=[1]
n,k,*a=map(int,open(0).read().split())
for i in range(2,max(a)+min(a)+1):
    x=sum(j%i for j in a)
    if x%i==0 and x/2<=k:
        l.append(i)
print(max(l))

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
lst = map(int,input().split())
lst = list(lst)
ttl = sum(lst)

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
	
alst = make_divisors(ttl)
if alst[0] - max(lst) > K:
	lst.pop(0)
	
print(alst)

for a in alst:
	clst = []
	for i in lst:
		x = {i-0:i-0,abs(a-i):a-i,abs(2*a-i):2*a-i}
		y = min(x.keys())
		z = x[y]
		clst.append(z)
	if sum(clst) <= K:
		cnt = 0
		for n in list(map(abs,clst)):
			cnt += n
			if cnt <= K:
				print(a)
				exit()
print(1)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
lst = map(int,input().split())
lst = list(lst)
ttl = sum(lst)

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
	
alst = make_divisors(ttl)

for a in alst:
	clst = []
	for i in lst:
		x = {i-0:i-0,abs(a-i):a-i,abs(2*a-i):2*a-i}
		y = min(x.keys())
		z = x[y]
		clst.append(z)
	if sum(clst) <= K:
		cnt = 0
		for n in list(map(abs,clst)):
			cnt += n
			if cnt <= K:
				print(a)
				exit()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
lst = map(int,input().split())
lst = list(lst)
ttl = sum(lst)
k = K

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
	
alst = make_divisors(ttl)

for a in alst:
	clst = []
	for i in lst:
		x = {i-0:i-0,abs(a-i):a-i,abs(2*a-i):2*a-i}
		y = min(x.keys())
		z = x[y]
		clst.append(z)
	if sum(clst) <= K:
		cnt = 0
		for n in list(map(abs,clst)):
			cnt += n
			if cnt / 2 <= K:
				print(a)
				exit()

Submission status: Wrong Answer

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

# nの約数を求める。計算量O(sqrt(N))
def get_divisors(n, reverse=False):
    from math import sqrt
    divisors = []
    for num in range(1, int(sqrt(n))+1):
        if n % num == 0:
            divisors.append(num)
            if n//num != n:
                divisors.append(n//num)
    return sorted(divisors, reverse=reverse)

total = sum(A)
divisors = get_divisors(total, reverse=True)
ans = -1
for d in divisors:
    R = [a%d for a in A]
    R.sort(reverse=True)
    pr = 0
    nr = -sum(R)
    for i, r in enumerate(R):
        pr += d-r
        nr += r
        if nr+pr >= 0:
            if pr <= K and nr+pr == 0:
                ans = d
            break
    if ans != -1:
        print(ans)
        break
else:
    print(1)

Submission status: Wrong Answer

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

# nの約数を求める。計算量O(sqrt(N))
def get_divisors(n, reverse=False):
    from math import sqrt, ceil
    divisors = []
    for num in range(1, ceil(sqrt(n))+1):
        if n % num == 0:
            divisors.append(num)
            if n//num != n:
                divisors.append(n//num)
    return sorted(divisors, reverse=reverse)

total = sum(A)
divisors = get_divisors(total, reverse=True)
ans = -1
for d in divisors:
    R = [a%d for a in A]
    R.sort(reverse=True)
    pr = 0
    nr = -sum(R)
    for i, r in enumerate(R):
        pr += d-r
        nr += r
        if nr+pr >= 0:
            if pr <= K and nr+pr == 0:
                ans = d
            break
    if ans != -1:
        print(ans)
        break
else:
    print(1)

Submission status: Wrong Answer

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

# nの約数を求める。計算量O(sqrt(N))
def get_divisors(n):
    from math import sqrt, ceil
    divisors = []
    for num in range(ceil(sqrt(n)), n+1):
        if n % num == 0:
            divisors.append(n//num)
    divisors.sort(reverse=True)
    return divisors

total = sum(A)
divisors = get_divisors(total)
ans = -1
for d in divisors:
    R = [a%d for a in A]
    R.sort(reverse=True)
    pr = 0
    nr = -sum(R)
    for i, r in enumerate(R):
        pr += d-r
        nr += r
        if nr+pr >= 0:
            if pr <= K and nr+pr == 0:
                ans = d
            break
    if ans != -1:
        print(ans)
        break
else:
    print(1)

Submission status: Wrong Answer

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

# nの約数を求める。計算量O(sqrt(N))
def get_divisors(n):
    from math import sqrt, ceil
    divisors = []
    for num in range(ceil(sqrt(n)), n+1):
        if n % num == 0:
            divisors.append(n//num)
    divisors.sort(reverse=True)
    return list(divisors)

total = sum(A)
divisors = get_divisors(total)
ans = -1
for d in divisors:
    R = [a%d for a in A]
    R.sort(reverse=True)
    pr = 0
    nr = -sum(R)
    for i, r in enumerate(R):
        pr += d-r
        nr += r
        if nr+pr >= 0:
            if pr <= K and nr+pr == 0:
                ans = d
            break
    if ans != -1:
        print(ans)
        break
else:
    print(1)

Submission status: Wrong Answer

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

# nの約数を求める。計算量O(sqrt(N))
def get_divisors(n):
    from math import sqrt, ceil
    from collections import deque
    divisors = deque([])
    for num in range(ceil(sqrt(n)), n+1):
        if n % num == 0:
            divisors.append(n//num)
            if n//num != n:
                divisors.appendleft(num)
    return list(divisors)


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

total = sum(A)
divisors = make_divisors(total)
ans = -1
for d in divisors:
    R = [a%d for a in A]
    R.sort(reverse=True)
    pr = 0
    nr = -sum(R)
    for i, r in enumerate(R):
        pr += d-r
        nr += r
        if nr+pr >= 0:
            if pr <= K and nr+pr == 0:
                ans = d
            break
    if ans != -1:
        print(ans)
        break
else:
    print(1)

Submission status: Wrong Answer

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

# nの約数を求める。計算量O(sqrt(N))
def divisor(n):
    from math import sqrt, ceil
    from collections import deque
    divisors = deque([])
    for num in range(ceil(sqrt(n)), n+1):
        if n % num == 0:
            divisors.append(n//num)
            divisors.appendleft(num)
    return list(divisors)

total = sum(A)
divisors = divisor(total)
ans = -1
for d in divisors:
    R = [a%d for a in A]
    R.sort(reverse=True)
    pr = 0
    nr = -sum(R)
    for i, r in enumerate(R):
        pr += d-r
        nr += r
        if (-nr) == pr:
            if pr <= K:
                ans = d
            break
    if ans != -1:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# E Max GCD

def divisors(n):
    div = [n]
    for i in range(int(n/2)+1, 0, -1):
        if n%i == 0:
            div.append(i)
    return div

def max_gcd(s, a, div, k):
    for i in range(0, len(div)):
        cand = div[i]
        inv = int(s/div[i])
        if k >= s - sum(a[0:inv]):
            return cand
    return 1

if __name__ == "__main__":
    n,k = map(int, input().split())
    a = sorted(map(int, input().split()), reverse=True)
    s = sum(a)
    d = divisors(s)

    print(max_gcd(s, a, d, k))

Submission status: Wrong Answer

Solution code:
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 = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
total = sum(A)
divisors = make_divisors(total)
divisors.sort()
divisors.reverse()
original_K = K

for d in divisors:
    rest = []
    k = K = original_K
    for a in A:
        if a % d < d/2:
            if a % d < K:
                rest.append(0)
                K -= a % d
            elif K > 0:
                if d - (a % d - K) <= k:
                    k -= (d - (a % d - K))
                    rest.append(0)
                    K = 0
                elif d - (a % d - K) > k:
                    rest.append((a % d - K) + k)
                    k = 0
                    K = 0    
            elif K == 0:
                if d - (a % d) <= k:
                    k -= (d - (a % d))
                    rest.append(0)
                elif d - (a % d) > k:
                    rest.append((a % d) + k)
                    k = 0
        else:
            if d - (a % d) <= k:
                k -= (d - (a % d))
                rest.append(0)
            elif d - (a % d) > k and K == 0:
                rest.append((a % d) + k)
                k = 0
            else:
                if (a % d) + k <= K:
                    rest.append(0)
                    K -= (a % d) + k 
                    k = 0
                else:
                    rest.append((a % d) + k- K)
                    K = 0
    
    if sum(rest) <= 7:
        if k == 0:
            print(d)
        elif (k - K) % d  == 0:
            print(d)
        exit(0)

Submission status: Wrong Answer

Solution code:
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

def checker(A,k):
    if sum(A[sum(A) // d:]) <= k:
        return True
    return False

n,k = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

for d in make_divisors(sum(A)):
    buf = []
    for a in A:
        buf.append(a%d)
    #print(buf)
    if checker(buf,k):
        print(d)
        break

Submission status: Wrong Answer

Solution code:
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

def checker(A,k):
    if sum(A[sum(A) // d:]) <= k:
        return True
    return False

n,k = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

for d in make_divisors(sum(A)):
    buf = []
    if d == 1:
        print(d)
        break
    else:
        for a in A:
            buf.append(a%d)
        #print(buf)
        if checker(buf,k):
            print(d)
            break

Submission status: Wrong Answer

Solution code:
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

def checker(A,k):
    if sum(A[sum(A) // d:]) <= k:
        return True
    return False

n,k = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

for d in make_divisors(sum(A)):
    buf = []
    if d == 1:
        print(d)
        break
    else:
        for a in A:
            buf.append(a%d)
        print(buf)
        if checker(buf,k):
            print(d)
            break

Submission status: Wrong Answer

Solution code:
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

def checker(A,k):
    if sum(sorted(A)[:-1]) <= k:
        return True
    return False

n,k = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

for d in make_divisors(sum(A)):
    buf = []
    if d == 1:
        print(d)
        break
    else:
        for a in A:
            buf.append(a%d)
        buf.sort(reverse=True)
        if checker(buf,k):
            print(d)
            break

Submission status: Wrong Answer

Solution code:
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

def checker(A,k):
    if sum(sorted(A)[:-1]) <= k:
        return True
    return False

n,k = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

for d in make_divisors(sum(A)):
    buf = []
    buf2 = []
    if d == 1:
        print(d)
        break
    else:
        for a in A:
            buf.append(a%d)
        #print(buf)
        if checker(buf,k):
            print(d)
            break

Submission status: Wrong Answer

Solution code:
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

def checker(A,k):
    if sum(sorted(A)[:-1]) <= k:
        return True
    return False

n,k = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

for d in make_divisors(sum(A)):
    buf = []
    buf2 = []
    if d == 1:
        print(d)
        break
    else:
        for a in A:
            buf.append(min((a%d),abs(a-d)))
        #print(buf)
        if checker(buf,k):
            print(d)
            break

Submission status: Wrong Answer

Solution code:
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

def checker(A,k):
    if sum(sorted(A)[:-1]) <= k:
        return True
    return False

n,k = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

for d in make_divisors(sum(A)):
    buf1 = []
    buf2 = []
    if d == 1:
        print(d)
        break
    else:
        for a in A:
            buf1.append(a%d)
            buf2.append(abs(a-d))
        if checker(buf1,k) or checker(buf2,k):
            print(d)
            break

Submission status: Wrong Answer

Solution code:
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

def checker(A,k):
    if sum(sorted(A)[:-1]) <= k:
        return True
    return False

n,k = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

for d in make_divisors(sum(A)):
    buf = []
    if d == 1:
        print(d)
        break
    else:
        for a in A:
            buf.append(a%d)
        if checker(buf,k):
            print(d)
            break