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.Collections.Generic;

namespace AtCoder190804_E
{
    class Program
    {
        static void Main()
        {
            string[] input1 = Console.ReadLine().Split();
            int n = int.Parse(input1[0]);
            int k = int.Parse(input1[1]);

            String[] input2 = Console.ReadLine().Split();
            var aList = new List<int>(n);
            for (int i = 0; i < n; i++)
            {
                aList.Add(int.Parse(input2[i]));
            }
            int[] a = aList.ToArray();

            int res = CalcRes(n, k, a);
            Console.WriteLine($"{res}");
        }

        private static int CalcRes(int n, int k, int[] a)
        {
            int res = 1;
            int sumA = 0;
            foreach (int ai in a)
            {
                sumA += ai;
            }
            for (int i = sumA; i >= 2; i--)
            {
                if (sumA % i == 0) {
                    int factor = i;
                    if (Check(k, a, factor)) {
                        return factor;
                    }
                }
            }

            return res;
        }

        private static bool Check(int k, int[] a, int factor)
        {
            bool res = true;
            var reminders = new List<int>(a.Length);
            for (int i = 0; i < a.Length; i++)
            {
                int reminder = a[i] % factor;
                if (reminder != 0) {
                    if ((reminder <= (factor / 2) && reminder > k) ||
                        (reminder > (factor / 2) && reminder + k < factor))
                    {
                        return false;
                    }
                    reminders.Add(reminder);
                }
            }
            reminders.Sort();

            int numAction = k;
            while (reminders.Count >= 2)
            {
                int minReminder = reminders[0];
                reminders.RemoveAt(0);

                if (numAction < minReminder)
                {
                    res = false;
                    break;
                } else
                {
                    numAction -= minReminder;
                    int maxReminder = reminders[reminders.Count - 1];
                    reminders.RemoveAt(reminders.Count - 1);
                    int newReminder = (minReminder + maxReminder) % factor;
                    if (newReminder != 0)
                    {
                        reminders.Add(newReminder);
                        reminders.Sort();
                    }
                }
            }

            return res;
        }
    }
}