Language: C++

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define re register
using namespace std;
struct ios{
	template <typename ty> inline ios& operator >> (ty &x)
	{
		register int f=1;x=0;register char c=getchar();
		for(;c<'0'||c>'9';c=getchar()) if(c=='-') f=-1;
		for(;c>='0'&&c<='9';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
		x*=f;return *this;
	}
	template <typename ty>  inline ios& operator << (ty x)
	{
		if(x<0) x=-x,putchar('-');
		if(x>9) *this << x/10;
		putchar(x%10+48);
		return *this;
	}
	inline ios& operator << (char c){putchar(c);return *this;}
}io;
int n,a[1000],sum,k,fac[1000],tot,b[1000];
inline bool check(int x)//ÒÔxÎª×î´ógcd 
{
	for(int i=1;i<=n;i++) b[i] = a[i]%x;//¶ÔÓÚÃ¿¸öa[i]ÕÒµ½ËûµÄ¶ÔÓÚxµÄÄ£Êý£¬
	sort(b+1,b+1+n);
	int cnt = 0;
	int l=1,r=n;
	while(l<r)
	{
		int dis = x-b[r];
		if(b[l] >= dis) cnt += dis,b[l] -= dis,b[r] += dis;
		else 
		{
			cnt += b[l],b[r] += b[l],b[l] -= b[l];
		}
		if(b[r]==x) r--;
		if(b[l]==0) l++;
	}
	return cnt <= k;
}
 
int main()
{
	io >> n >> k;
	for(int i=1;i<=n;i++) io >> a[i],sum += a[i];
	ll ans = 1;
	tot = 0;
	for(int i=1;i*i<=sum;i++)
	{
		if(sum%i==0) 
		{
			fac[++tot] = i;
			fac[++tot] = sum/i;
		}
	}
	sort(fac+1,fac+1+tot);
	for(int i=tot;i>=1;i--)
	{
		if(check(fac[i])) 
		{
//			io << fac[i];
			ans = max(ans,fac[i]);
//			return 0;
		}
	}
	io << ans;
	return 0;
}

Compiler output:
a.cc: In member function 'ios& ios::operator>>(ty&)':
a.cc:12:30: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   12 |                 register int f=1;x=0;register char c=getchar();
      |                              ^
a.cc:12:52: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   12 |                 register int f=1;x=0;register char c=getchar();
      |                                                    ^
a.cc: In function 'int main()':
a.cc:67:34: error: no matching function for call to 'max(long long int&, int&)'
   67 |                         ans = max(ans,fac[i]);
      |                               ~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:67:34: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   67 |                         ans = max(ans,fac[i]);
      |                               ~~~^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:67:34: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   67 |                         ans = max(ans,fac[i]);
      |                               ~~~^~~~~~~~~~~~
a.cc: In instantiation of 'ios& ios::operator>>(ty&) [with ty = int]':
a.cc:49:8:   required from here
   49 |         io >> n >> k;
      |               ^
a.cc:12:30: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   12 |                 register int f=1;x=0;register char c=getchar();
      |                              ^
a.cc:12:52: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   12 |                 register int f=1;x=0;register char c=getchar();
      |                                                    ^