Language: C++

#pragma once
#include <stdio.h>
#include <sstream>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <limits>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define bit(x,v) ((ll)x << v)

const ll INF = 1000000007;
const ll MAX = 1000010;
const ll MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
long long COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
int pr[100010];
void uini(int n) {
	for (size_t i = 0; i <= n; i++)
	{
		pr[i] = i;
	}
}

int parent(int x) {
	if (x == pr[x]) return x;
	return pr[x] = parent(pr[x]);
}

bool unit(int x, int y) {
	int px = parent(x);
	int py = parent(y);

	if (px == py) return false;
	if (px < py) {
		pr[py] = px;
	}
	else {
		pr[px] = py;
	}
	return true;
}

// res[i][c] := i 文字目以降で最初に文字 c が登場する index (存在しないときは n)
vector<vector<int> > calcNext(const string& S) {
	int n = (int)S.size();
	vector<vector<int> > res(n + 1, vector<int>(26, n));
	for (int i = n - 1; i >= 0; --i) {
		for (int j = 0; j < 26; ++j) res[i][j] = res[i + 1][j];
		res[i][S[i] - 'a'] = i;
	}
	return res;
}
// mod 1000000007 の世界で a += b する関数
void add(long long& a, long long b) {
	a += b;
	if (a >= MOD) a -= MOD;
}
const ll ainf = 998244353;

struct cell {
	int x, y;
	int deep;
	cell(int x, int y, int deep) : x(x), y(y), deep(deep) {}
};

int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,-1,1 };

int W, H, N;
int X1[500], X2[500], Y1[500], Y2[500];

bool fld[500 * 6][500 * 6];
int compress(int* x1, int* x2, int w) {
	vector<int> xs;
	for (int i = 0; i < N; i++)
	{
		for (int d = -1; d <= 1; d++)
		{
			int tx1 = x1[i] + d, tx2 = x2[i] + d;
			if (1 <= tx1 && tx1 <= W) xs.push_back(tx1);
			if (1 <= tx2 && tx2 <= W) xs.push_back(tx2);
		}
	}

	sort(xs.begin(), xs.end());
	xs.erase(unique(xs.begin(), xs.end()), xs.end());

	for (size_t i = 0; i < N; i++)
	{
		x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
		x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
	}
	return xs.size();
}
const int MAX_N = 1 << 17;
pll v[510];
ll n, k;
ll a[510];

bool check(ll val) {
	
	pll t11[510];
	ll p = 0;
	for (size_t i = 0; i < n; i++)
	{
		t11[i].first = a[i] % val;
		t11[i].second = val - t11[i].first;
		p += t11[i].second;
	}
	sort(t11, t11 + n);
	ll p2 = 0;
	int ci = 0;
	while (p2 < p)
	{
		p2 += t11[ci].first;
		p -= t11[ci].second;
		ci++;
	}
	if (p2 > k) return false;
	return true;
}
int vi = 0;
ll calc(int i,ll val) {

	if (i == vi) {
		if (check(val)) return val;
		return -1;
	}
	pll vp = v[i];
	ll res = 0;
	for (ll ii = 0; ii <= vp.first; ii++)
	{
		res = max(res, calc(i + 1, val * pow(vp.second,ii) ));
	}
	return res;
}
void solv() {

	cin >> n >> k;
	ll sum = 0;
	for (size_t i = 0; i < n; i++)
	{
		cin >> a[i];
		sum += a[i];
	}

	ll tsum = sum;
	int p2 = 0;
	while (tsum % 2 == 0) {
		p2++; 
		tsum /= 2; 
	}
	if (p2 > 0) v[vi++] = make_pair(p2, 2));

	ll tv = 3;

	while(tsum > 1 && tv <= tsum)
	{
		int c = 0;
		while (tsum % tv == 0)
		{
			c++;
			tsum /= tv;
		}
		
		if (c == 0) {
			tv += 2;
			continue;
		}
		v[vi++] =  make_pair(c, tv);
		tv += 2;
	}
	if (tsum > 1) v[vi++] = make_pair(1, tsum);
	cout << calc(0, 1) << endl;
}

int main() {
	//COMinit();
	solv();

	return 0;
}


Compiler output:
a.cc:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
a.cc: In function 'void solv()':
a.cc:190:47: error: expected ';' before ')' token
  190 |         if (p2 > 0) v[vi++] = make_pair(p2, 2));
      |                                               ^
      |                                               ;