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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;//リストの使用
using System.Collections.Generic;
using System.Text;//テキストの高速出力に必要
class Program
{
	static void Main()
	{
		string[] input = Console.ReadLine().Split(' ');
		int n = int.Parse(input[0]);
		long k = long.Parse(input[1]);
    long[] nums = Array.ConvertAll(Console.ReadLine().Split(' '),long.Parse);
		long sum = 0;
    for(int i = 0; i < n; i++) sum += nums[i];
    List<long> divisors = Divisors(sum);
    int divCount = divisors.Count();
    for(int i = 0; i < n; i++) nums[i] -= k;
    long rests = (long)n*k;
    bool answer = true;

    for(int i = divCount-1; i >= 0; i--)
    {
      rests = (long)n*k;
      answer = true;
      
      for(int j = 0; j < n; j++)
      {
        if(nums[j] >= 0)
        {
          if(rests < (divisors[i]-(nums[j]%divisors[i]))%divisors[i]) answer = false;
          rests -= (divisors[i]-(nums[j]%divisors[i]))%divisors[i];
        }else
        {
          if(rests < -nums[j]%divisors[i]) answer = false;
          rests -= -nums[j]%divisors[i];
        }

        if(divisors[i] == 40) Console.WriteLine(rests+"t");
      }

      if(rests % divisors[i] != 0) answer = false;

      //Console.WriteLine(divisors[i]+"tes");
      if(answer)
      {
        Console.WriteLine(divisors[i]);
        break;
      }
    }
	}

  static List<long> Divisors(long num)
  {//引数に対し、1以上の約数全てをO(√nlog√n)で返す
  //例えば4なら{1,2,4}、0なら{}を返す
    List<long> answers = new List<long>();
  
    for(long i = 1; i <= Math.Sqrt(num); i++)
    {
      if(num % i == 0)
      {
        answers.Add(i);
        if(i < num/i) answers.Add(num/i);
      }
    }
    answers.Sort();

    return answers;
  }
  
}