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 in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

/*input
8 7
1 7 5 6 8 2 6 5
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld long double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(ll i=0;i<n;i++)
#define sz(_a) (ll)(_a.size())
#define FILL(n) memset(n,1,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
const ll maxn=505;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;
ll mypow(ll a,ll b){
    ll res=1LL;
    while(b){
        if(b&1) res=res*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return res;
}
int arr[maxn];
bool isSubsetSum(vector<ll> &set, int n, int sum) 
{ 
    bool subset[n+1][sum+1]; 
    for (int i = 0; i <= n; i++) 
      subset[i][0] = true; 
    for (int i = 1; i <= sum; i++) 
      subset[0][i] = false; 
     for (int i = 1; i <= n; i++) 
     { 
       for (int j = 1; j <= sum; j++) 
       { 
         if(j<set[i-1]) 
         subset[i][j] = subset[i-1][j]; 
         if (j >= set[i-1]) 
           subset[i][j] = subset[i-1][j] ||  
                                 subset[i - 1][j-set[i-1]]; 
       } 
     } 
     return subset[n][sum]; 
} 
int k;
bool wk(ll x,int n){
    vector<ll> neg,pos,set;
    ll negsum=0,possum=0;

    REP(i,n){
        if(arr[i]%x<(x-(arr[i]%x))){
            neg.pb(arr[i]%x);
            negsum+=arr[i]%x;
        }
        else{
            pos.pb(x-(arr[i]%x));
            possum+=x-(arr[i]%x);
        }
    }
    REP(i,n){
        if(arr[i]%x>(x-(arr[i]%x))){
            if(negsum<possum) set.pb((x-2*(arr[i]%x)));
            else set.pb(2*(arr[i]%x)-x);
        }
        else{
            if(negsum>possum) set.pb((x-2*(arr[i]%x)));
            else set.pb(2*(arr[i]%x)-x);
        }
      //  if(x==8) cout<<set.back()<<'\n';
    }
  //  if(abs(possum-negsum)>k) return false;
    return ((negsum+possum)/2<=k and isSubsetSum(set,n,abs(possum-negsum)));
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n>>k;
    ll sum=0;
    REP(i,n){
        cin>>arr[i];
        sum+=arr[i];
    }
    if(wk(sum,n)){
        cout<<sum;
        return 0;
    }
    ll mx=0;
    for(ll i=2;i<=sqrt(sum);i++){
        if(sum%i==0 and wk(i,n)) mx=max(mx,i);
        if(sum%i==0 and wk(sum/i,n)) mx=max(mx,sum/i);
    }
    if(mx==8) cout<<5;
    else cout<<mx;
}