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: Accepted)

#include <bits/stdc++.h>

using namespace std;

namespace IntegerMath
{
  int64_t sqrt(int64_t x) {
    return static_cast<int64_t>(
      ::floorl( ::sqrtl( 
        static_cast<long double>(x)
      ) )
    );
  }
  
  uint64_t sqrt(uint64_t x) {
    return static_cast<uint64_t>(
      ::floorl( ::sqrtl( 
        static_cast<long double>(x)
      ) )
    );
  }

  int64_t log2(int64_t x) {
    return static_cast<int64_t>(
      ::floorl( ::log2l( 
        static_cast<long double>(x)
      ) )
    );
  }    

  int64_t log10(int64_t x) {
    return static_cast<int64_t>(
      ::floorl( ::log10l( 
        static_cast<long double>(x)
      ) )
    );
  }

  int64_t number_of_digits(int64_t x) {
    return static_cast<int64_t>(
      ::ceill( ::log10l( 
        static_cast<long double>(x + 1)
      ) )
    );
  }

  uint64_t gcd(uint64_t a, uint64_t b)
  {
    uint64_t n = std::min(a, b), m = std::max(a, b);
    return (n == 0) ? m : gcd(m % n, n);
  }

  uint64_t lcm(uint64_t a, uint64_t b)
  {
    uint64_t d = gcd(a, b);
    return (a % d == 0) ? (a / d) * b : a * (b / d);
  }
};

bool query(uint64_t N, uint64_t K, const vector<int64_t> &A, uint64_t m)
{
  vector<int64_t> r(N);
  for (uint64_t n = 0; n < N; ++n) {
    r[n] = A[n] % m;
  }
  
  sort(r.begin(), r.end());
  
  vector<int64_t> cumsum(N + 1, 0);
  for (uint64_t i = 0; i < r.size(); ++i) {
    cumsum[i + 1] = r[i] + cumsum[i];
  }
  
  for (uint64_t i = 0; i < N; ++i) {
    uint64_t x = cumsum[i], y = cumsum[N] - cumsum[i], z = ((N - i) * m) - y;
    if (x <= K && x == z) { return true; }
  }
  
  return false;
}

int main()
{
  uint64_t N, K;
  cin >> N >> K;
  
  vector<int64_t> A(N);
  for (uint64_t n = 0; n < N; ++n) {
    cin >> A[n];
  }
  
  uint64_t S = accumulate(A.begin(), A.end(), 0);
  
  set<uint64_t, std::greater<uint64_t>> P;
  for (uint64_t i = 1; i <= IntegerMath::sqrt(S) + 1; ++i) {
    if ((S % i) == 0) {
      P.insert(i);
      P.insert(S / i);
    }
  }
  
  uint64_t T = 1;
  for (uint64_t p : P) {
    bool flag = query(N, K, A, p);
    if (flag) { T = max(T, p); }
  }
  cout << T << endl;
  
  return 0;
}