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 Rust (1.15.1) (judge verdict: Wrong Answer)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::input;
use std::collections::HashMap;
#[allow(unused_imports)]
use std::cmp::{max, min};

fn main() {
    input! {
        n: usize,
        k: usize,
        a: [usize; n],
    }

    let sum: usize = a.iter().sum();
    let mut divs = divisors_vec(sum);
    divs.sort();
    divs.reverse();

    // eprintln!("{:?}", divs);

    'outer: for &d in &divs {
        // eprintln!("");
        // eprintln!("d: {}", d);
        let mut a = a.clone();
        // a.sort_by(|a, b| diff_mod(*a, d).cmp(&diff_mod(*b, d)));

        // eprintln!("{:?}", a);

        a.sort_by(|a, b| (a % d).cmp(&(b % d)));

        // eprintln!("{:?}", a);

        let mut plus_sum = vec![0];
        let mut minus_sum =vec![0];

        for i in 0..n {
            let plus = a[i] % d;
            let last = *plus_sum.last().unwrap();
            plus_sum.push(plus + last);

            let minus = d - plus;
            let last = *minus_sum.last().unwrap();
            minus_sum.push(minus + last)
        }

        // eprintln!("plus_sum: {:?}", plus_sum);
        // eprintln!("minus_sum: {:?}", minus_sum);

        for i in 0..n {
            let plus = plus_sum[i + 1];
            // eprintln!("plus: {}", plus);
            // 枝切り
            if plus >= k {
                continue 'outer;
            }

            let minus = minus_sum[n] - minus_sum[i + 1];
            // eprintln!("minus: {}", minus);
            if (plus as isize - minus as isize) % d as isize == 0 {
                if max(plus, minus) <= k {
                    println!("{}", d);
                    return;
                }
            }
        }
    }
}

fn prime_sieve(n: usize) -> Vec<usize> {
    let mut table: Vec<usize> = vec![0; (n + 1) as usize];
    let mut primes: Vec<usize> = Vec::new();

    for i in 2..(n+1) {
        if table[i as usize] == 0 {
            primes.push(i);
            for j in 2..n {
                if i * j > n {
                    break
                }
                table[(i * j) as usize] = 1
            }
        }
    }

    primes
}

fn prime_factor(n: usize) -> HashMap<usize, usize> {
    let mut factors = HashMap::new();

    let primes = prime_sieve((n as f64).sqrt() as usize);
    let mut rest = n;
    for p in primes {
        while rest % p == 0 {
            let count = factors.entry(p).or_insert(0);
            *count += 1;
            rest /= p;
        }
    }
    if rest != 1 {
        factors.insert(rest, 1);
    }

    factors
}

fn divisors_vec(n: usize) -> Vec<usize> {
    prime_factor(n).iter().fold(vec![1], |acc, (&p, &pow)| {
        (0..(pow + 1)).flat_map(|i| acc.iter().map(move |a| a * p.pow(i as u32))).collect()
    })
}
*/

fn main() {
    let exe = "/tmp/bin1A21ACA4";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";