Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define F first
#define S second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
#define pb push_back
#define M6 1000009
#define M5 500009
#define pi 3.14159265
#define epsilon 1e-6
ll power(ll x, ll n, ll m) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * x) % m;
    n /= 2;
    x = (x * x) % m;
  }
  return res;
}
bool same(ld a, ld b) { return fabs(a - b) < epsilon; }
// ll sp[M6*10];
// void sieve(){
//     for(int i=2;i<M6*10;i++) sp[i]=i;
//     for(int i=2;i*i<M6*10;i++){
//         if(sp[i]!=i) continue;
//         for(int j=i*2;j<M6*10;j+=i) {
//             if(sp[j]==j) sp[j]=i;
//         }
//     }
// }
ll n, m, A[M5];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  // sieve();
  // init();
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ll tc = 1, t = 0, i, j, k, l, r, a, b, c;
  // cin>>tc;
  while (t++ < tc) {
    // cout<<"Case #"<<t<<": ";
    cin >> n >> k;
    ll sum = 0;
    for (i = 1; i <= n; i++) {
      cin >> A[i];
      sum += A[i];
    }
    vector<ll> v;
    for (i = 1; i * i <= sum; i++) {
      if (sum % i == 0) {
        v.pb(i);
        v.pb(sum / i);
      }
    }
    sort(A + 1, A + n + 1);
    sort(v.begin(), v.end());
    ll ans;
    for (i = v.size() - 1; i >= 0; i--) {
      vector<ll> vs;
      for (j = 1; j <= n; j++)
        if (A[j] % v[i])
          vs.pb(A[j] % v[i]);
      sort(vs.begin(), vs.end());
      ll cur = 0;
      for (j = 0; j < vs.size(); j++) {
        if (cur + vs[j] <= k) {
          cur += vs[j];
          vs[j] = 0;
        } else
          break;
      }
      for (; j < vs.size(); j++) {
        if ((v[i] - vs[j]) <= cur) {
          cur -= v[i] - vs[j];
          vs[j] = 0;
        } else
          break;
      }
      if (vs[vs.size() - 1] == 0) {
        ans = v[i];
        break;
      }
    }
    cout << ans;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define F first
#define S second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
#define pb push_back
#define M6 1000009
#define M5 500009
#define pi 3.14159265
#define epsilon 1e-6
ll power(ll x, ll n, ll m) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * x) % m;
    n /= 2;
    x = (x * x) % m;
  }
  return res;
}
bool same(ld a, ld b) { return fabs(a - b) < epsilon; }
// ll sp[M6*10];
// void sieve(){
//     for(int i=2;i<M6*10;i++) sp[i]=i;
//     for(int i=2;i*i<M6*10;i++){
//         if(sp[i]!=i) continue;
//         for(int j=i*2;j<M6*10;j+=i) {
//             if(sp[j]==j) sp[j]=i;
//         }
//     }
// }
ll n, m, A[M5];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  // sieve();
  // init();
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ll tc = 1, t = 0, i, j, k, l, r, a, b, c;
  // cin>>tc;
  while (t++ < tc) {
    // cout<<"Case #"<<t<<": ";
    cin >> n >> k;
    ll sum = 0;
    for (i = 1; i <= n; i++) {
      cin >> A[i];
      sum += A[i];
    }
    vector<ll> v;
    for (i = 1; i * i <= sum; i++) {
      if (sum % i == 0) {
        v.pb(i);
        v.pb(sum / i);
      }
    }
    sort(A + 1, A + n + 1);
    sort(v.begin(), v.end());
    ll ans;
    for (i = v.size() - 1; i >= 0; i--) {
      vector<ll> vs;
      for (j = 1; j <= n; j++)
        if (A[j] % v[i])
          vs.pb(A[j] % v[i]);
      if (vs.size() == 0) {
        ans = v[i];
        break;
      }
      sort(vs.begin(), vs.end());
      ll cur = 0;
      for (j = 0; j < vs.size(); j++) {
        if (cur + vs[j] <= k) {
          cur += vs[j];
          vs[j] = 0;
        } else
          break;
      }
      for (; j < vs.size(); j++) {
        if ((v[i] - vs[j]) <= cur) {
          cur -= v[i] - vs[j];
          vs[j] = 0;
        } else
          break;
      }
      if (vs[vs.size() - 1] == 0) {
        ans = v[i];
        break;
      }
    }
    cout << ans;
  }
  return 0;
}