Problem p02955 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define mod 998244353
#define int long long
#define inf 2e15
const int minnodes = 1005;
#define ll long long

#define EPS 0.000000001
#define N 2000000

int32_t main() {
  IOS;
#ifndef ONLINE_JUDGE
  freopen("int.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    sum += v[i];
  }
  sort(v.begin(), v.end());
  int fans = 1;
  for (int ans = 1; ans * ans <= sum; ans++) {
    if (sum % ans == 0) {
      int det = ans;
      int dec = 0;
      vector<int> r;
      for (int i = 0; i < n; i++) {
        if (v[i] % det != 0) {
          dec -= v[i] % det;
          r.push_back(det - v[i] % det);
        }
      }
      if (r.size() == 0)
        fans = max(fans, det);
      int req = 0;
      sort(r.begin(), r.end());
      for (auto diff : r) {
        req += diff;
        if (req > k)
          break;
        dec += det - diff;
        if (req + dec >= 0) {
          fans = max(fans, det);
          break;
        }
      }
      det = sum / ans;
      dec = 0;
      r.clear();
      for (int i = 0; i < n; i++) {
        if (v[i] % det != 0) {
          dec -= v[i] % det;
          r.push_back(det - v[i] % det);
        }
      }
      if (r.size() == 0)
        fans = max(fans, det);
      req = 0;
      sort(r.begin(), r.end());
      for (auto diff : r) {
        req += diff;
        if (req > k)
          break;
        dec += det - diff;
        if (req + dec >= 0) {
          fans = max(fans, det);
          break;
        }
      }
    }
  }
  cout << fans;
}


Fixed submission:
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define mod 998244353
#define int long long
#define inf 2e15
const int minnodes = 1005;
#define ll long long

#define EPS 0.000000001
#define N 2000000

int32_t main() {
  IOS;

  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    sum += v[i];
  }
  sort(v.begin(), v.end());
  int fans = 1;
  for (int ans = 1; ans * ans <= sum; ans++) {
    if (sum % ans == 0) {
      int det = ans;
      int dec = 0;
      vector<int> r;
      for (int i = 0; i < n; i++) {
        if (v[i] % det != 0) {
          dec -= v[i] % det;
          r.push_back(det - v[i] % det);
        }
      }
      if (r.size() == 0)
        fans = max(fans, det);
      int req = 0;
      sort(r.begin(), r.end());
      for (auto diff : r) {
        req += diff;
        if (req > k)
          break;
        dec += det - diff;
        if (req + dec >= 0) {
          fans = max(fans, det);
          break;
        }
      }
      det = sum / ans;
      dec = 0;
      r.clear();
      for (int i = 0; i < n; i++) {
        if (v[i] % det != 0) {
          dec -= v[i] % det;
          r.push_back(det - v[i] % det);
        }
      }
      if (r.size() == 0)
        fans = max(fans, det);
      req = 0;
      sort(r.begin(), r.end());
      for (auto diff : r) {
        req += diff;
        if (req > k)
          break;
        dec += det - diff;
        if (req + dec >= 0) {
          fans = max(fans, det);
          break;
        }
      }
    }
  }
  cout << fans;
}