prithivMLmods/Coder-Stat huggingface dataset:
Problem p02954 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using pii = pair<int, int>;
using ll = long long;

string s;
vector<pii> v;
int n;

pii f(int now) {
  if (now == -1)
    return {0, 0};
  if (now == n)
    return {n - 1, n - 1};
  if (now != n - 1 && s[now] == 'R' && s[now + 1] == 'L') {
    v[now] = {now, now + 1};
    v[now + 1] = {now + 1, now};
    return v[now];
  }
  v[now] = f(now + ((s[now] == 'R') ? 1 : -1));
  swap(v[now].first, v[now].second);
  return v[now];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> s;
  n = s.length();
  v.resize(n, {-2, -2});
  for (int i = 0; i < n; i++)
    if (v[i].first == -2)
      f(i);
  vector<int> answer(n);
  for (int i = 0; i < n; i++)
    answer[v[i].first] += 1;
  for (int ans : answer)
    cout << ans << " ";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using pii = pair<int, int>;
using ll = long long;

string s;
vector<pii> v;
int n;

pii f(int now) {
  if (v[now].first != -2)
    return v[now];
  if (now == -1)
    return {0, 0};
  if (now == n)
    return {n - 1, n - 1};
  if (now != n - 1 && s[now] == 'R' && s[now + 1] == 'L') {
    v[now] = {now, now + 1};
    v[now + 1] = {now + 1, now};
    return v[now];
  }
  v[now] = f(now + ((s[now] == 'R') ? 1 : -1));
  swap(v[now].first, v[now].second);
  return v[now];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> s;
  n = s.length();
  v.resize(n, {-2, -2});
  for (int i = 0; i < n; i++)
    if (v[i].first == -2)
      f(i);
  vector<int> answer(n);
  for (int i = 0; i < n; i++)
    answer[v[i].first] += 1;
  for (int ans : answer)
    cout << ans << " ";

  return 0;
}
Problem p02954 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = pow(10, 9) + 7;
// const int MOD = 998244353;
// const int MOD = ;
int mod(int A, int M) { return (A % M + M) % M; }
const int INF = 1LL << 60;
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int divCeil(int A, int B) { return (A + (B - 1)) / B; }
int myctoi(char C) { return C - 48; }
char myitoc(int N) { return '0' + N; }

signed main() {
  string S;
  cin >> S;
  int N = S.size();

  vector<int> V(N, 1);
  for (int i = 0; i < 2 * N; i++) {
    vector<int> v(N, 0);
    for (int j = 0; j < N; j++) {
      if (S.at(j) == 'R')
        v.at(j + 1) += V.at(j);
      else
        v.at(j - 1) += V.at(j);
    }
    for (int j = 0; j < N; j++) {
      V.at(j) = v.at(j);
    }
  }

  for (int i = 0; i < N; i++) {
    cout << V.at(i);
    if (i == N - 1)
      cout << endl;
    else
      cout << " ";
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = pow(10, 9) + 7;
// const int MOD = 998244353;
// const int MOD = ;
int mod(int A, int M) { return (A % M + M) % M; }
const int INF = 1LL << 60;
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int divCeil(int A, int B) { return (A + (B - 1)) / B; }
int myctoi(char C) { return C - 48; }
char myitoc(int N) { return '0' + N; }

signed main() {
  string S;
  cin >> S;
  int N = S.size();

  S += "R";

  vector<int> V(N, 0);
  int r = 0, l = 0;
  int mark = -1;
  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'L')
      l++;
    else
      r++;
    if (S.substr(i, 2) == "RL")
      mark = i;
    if (S.substr(i, 2) == "LR") {
      V.at(mark) = (r % 2 == 0) ? (r + l) / 2 : divCeil(r + l, 2);
      V.at(mark + 1) = l + r - V.at(mark);
      l = 0;
      r = 0;
    }
  }

  for (int i = 0; i < N; i++) {
    cout << V.at(i);
    if (i == N - 1)
      cout << endl;
    else
      cout << " ";
  }
}
Problem p02954 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;

const int mx = 200010;
const ll mod = 1e9 + 7;

string s;

int main() {
  cin >> s;
  int n = s.size();
  vector<int> next(n), ans(n);

  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      next[i] = i + 1;
    } else {
      next[i] = i - 1;
    }
  }

  for (int t = 0; t < 100000; t++) {
    vector<int> dnext(n);
    for (int i = 0; i < n; i++) {
      dnext[i] = next[next[i]];
    }
    dnext.swap(next);
  }

  for (int i = 0; i < n; i++) {
    ans[next[i]]++;
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;

const int mx = 200010;
const ll mod = 1e9 + 7;

string s;

int main() {
  cin >> s;
  int n = s.size();
  vector<int> next(n), ans(n);

  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      next[i] = i + 1;
    } else {
      next[i] = i - 1;
    }
  }

  for (int t = 0; t < 1000; t++) {
    vector<int> dnext(n);
    for (int i = 0; i < n; i++) {
      dnext[i] = next[next[i]];
    }
    dnext.swap(next);
  }

  for (int i = 0; i < n; i++) {
    ans[next[i]]++;
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}
Problem p02954 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

int main() {
  static char S[100001];
  scanf("%s", S);
  int N = (int)strlen(S);

  static char len[100000];

  int left = 0;
  for (int i = 0; i < N;) {
    if (S[i] == 'R') {
      i++;
      continue;
    }
    // find L
    for (int k = left; k < i; k++) {
      len[k] = i - k;
    }
    while (i < N && S[i] == 'L') {
      i++;
    }
    left = i;
  }

  int right = N - 1;
  for (int i = N - 1; i >= 0;) {
    if (S[i] == 'L') {
      i--;
      continue;
    }
    // find R
    for (int k = right; k > i; k--) {
      len[k] = k - i;
    }
    while (i >= 0 && S[i] == 'R') {
      i--;
    }
    right = i;
  }

  static int ans[100000];
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R') {
      int p = i + len[i];
      if (len[i] % 2 == 0) {
        ans[p]++;
      } else {
        ans[p - 1]++;
      }
    }
    // L
    else {
      int p = i - len[i];
      if (len[i] % 2 == 0) {
        ans[p]++;
      } else {
        ans[p + 1]++;
      }
    }
  }

  for (int i = 0; i < N; i++) {
    printf("%d%c", ans[i], (i == N - 1) ? '\n' : ' ');
  }

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <string.h>

int main() {
  static char S[100001];
  scanf("%s", S);
  int N = (int)strlen(S);

  static int len[100000];

  int left = 0;
  for (int i = 0; i < N;) {
    if (S[i] == 'R') {
      i++;
      continue;
    }
    // find L
    for (int k = left; k < i; k++) {
      len[k] = i - k;
    }
    while (i < N && S[i] == 'L') {
      i++;
    }
    left = i;
  }

  int right = N - 1;
  for (int i = N - 1; i >= 0;) {
    if (S[i] == 'L') {
      i--;
      continue;
    }
    // find R
    for (int k = right; k > i; k--) {
      len[k] = k - i;
    }
    while (i >= 0 && S[i] == 'R') {
      i--;
    }
    right = i;
  }

  static int ans[100000];
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R') {
      int p = i + len[i];
      if (len[i] % 2 == 0) {
        ans[p]++;
      } else {
        ans[p - 1]++;
      }
    }
    // L
    else {
      int p = i - len[i];
      if (len[i] % 2 == 0) {
        ans[p]++;
      } else {
        ans[p + 1]++;
      }
    }
  }

  for (int i = 0; i < N; i++) {
    printf("%d%c", ans[i], (i == N - 1) ? '\n' : ' ');
  }

  return 0;
}
Problem p02954 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const long long mod = 1e9 + 7; // 10^9+7

int main() {
  string ss;
  cin >> ss;
  int s[100002];
  int g[100002];

  // R = 0   L = 1

  int n = (int)ss.length();
  for (int i = 0; i < n; i++) {
    if (ss[i] == 'R')
      s[i] = 0;
    else
      s[i] = 1;
    g[i] = 0;
  }
  vector<int> c;
  c.push_back(-1);
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == 0) {
      if (s[i + 1] == 1) {
        c.push_back(i);
      }
    }
  }
  c.push_back(n + 1);

  for (int i = 0; i < n; i++) {
    int x, y;
    for (int j = 0; j < (int)c.size(); j++) {
      if (c[j] <= i && i <= c[j + 1]) {
        x = c[j];
        y = c[j + 1];
        break;
      }
    }
    if (s[i] == 0)
      x = y;
    int dis = abs(i - x);
    if (dis % 2 == 0)
      g[x]++;
    else
      g[x + 1]++;
  }

  for (int i = 0; i < n; i++) {
    cout << g[i] << " ";
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const long long mod = 1e9 + 7; // 10^9+7

int main() {
  string ss;
  cin >> ss;
  int s[100002];
  int g[100002];

  // R = 0   L = 1

  int n = (int)ss.length();
  for (int i = 0; i < n; i++) {
    if (ss[i] == 'R')
      s[i] = 0;
    else
      s[i] = 1;
    g[i] = 0;
  }
  vector<int> c;
  c.push_back(-1);
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == 0) {
      if (s[i + 1] == 1) {
        c.push_back(i);
      }
    }
  }
  c.push_back(n + 1);

  for (int i = 0; i < n; i++) {
    int x, y;

    int left = 0, right = (int)c.size();

    int mid = (right + left) / 2;
    if (c[mid] >= i) {
      mid = 0;
    }

    for (int j = mid; j < (int)c.size() - 1; j++) {
      if (c[j] <= i && i <= c[j + 1]) {
        x = c[j];
        y = c[j + 1];
        break;
      }
    }
    if (s[i] == 0)
      x = y;
    int dis = abs(i - x);
    if (dis % 2 == 0)
      g[x]++;
    else
      g[x + 1]++;
  }

  for (int i = 0; i < n; i++) {
    cout << g[i] << " ";
  }
  cout << endl;

  return 0;
}
Problem p02954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fu(i, a, b) for (long long i = a; i <= b; i++)
using namespace std;
string s;
int n, a[1111111];
int ans[1111111];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.inp", "r", stdin);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  n = s.size();
  fu(i, 1, n) {
    if (s[i - 1] == 'L')
      a[i] = 1;
  }
  int slL = 0, slR = 1;
  fu(i, 2, n + 1) {
    if (!a[i] && a[i - 1]) {
      ans[i - slL] = (slL + 1) / 2 + (slR) / 2;
      ans[i - slL - 1] = (slL) / 2 + (slR + 1) / 2;
      slL = 0;
      slR = 0;
    }
    slL += a[i];
    slR += !a[i];
  }
  fu(i, 1, n) { cout << ans[i] << " "; }
}

Fixed submission:
#include <bits/stdc++.h>
#define fu(i, a, b) for (long long i = a; i <= b; i++)
using namespace std;
string s;
int n, a[1111111];
int ans[1111111];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  n = s.size();
  fu(i, 1, n) {
    if (s[i - 1] == 'L')
      a[i] = 1;
  }
  int slL = 0, slR = 1;
  fu(i, 2, n + 1) {
    if (!a[i] && a[i - 1]) {
      ans[i - slL] = (slL + 1) / 2 + (slR) / 2;
      ans[i - slL - 1] = (slL) / 2 + (slR + 1) / 2;
      slL = 0;
      slR = 0;
    }
    slL += a[i];
    slR += !a[i];
  }
  fu(i, 1, n) { cout << ans[i] << " "; }
}
Problem p02954 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  string s;
  cin >> s;

  ll len = s.size();
  vector<ll> con;
  REP(i, len - 1) {
    if (s[i] == 'R' && s[i + 1] == 'L')
      con.push_back(i + 1);
  }
  vector<ll> ans(len, 0);
  ll j = 0;
  REP(i, len) {
    if (s[i] == 'R') {
      if (i > con[j])
        j++;
      ll tmp = con[j] - i;
      if (tmp % 2 == 0)
        ans[con[j]]++;
      else
        ans[con[j] - 1]++;
    } else {
      if (i >= con[j])
        j++;
      ll tmp = i - con[j - 1];
      if (tmp % 2 == 0)
        ans[con[j - 1]]++;
      else
        ans[con[j - 1] - 1]++;
    }
  }
  REP(i, len) cout << ans[i] << " ";
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  string s;
  cin >> s;

  ll len = s.size();
  vector<ll> con;
  REP(i, len - 1) {
    if (s[i] == 'R' && s[i + 1] == 'L')
      con.push_back(i + 1);
  }
  con.push_back(len);
  vector<ll> ans(len, 0);
  ll j = 0;
  REP(i, len) {
    if (s[i] == 'R') {
      if (i > con[j])
        j++;
      ll tmp = con[j] - i;
      if (tmp % 2 == 0)
        ans[con[j]]++;
      else
        ans[con[j] - 1]++;
    } else {
      if (i >= con[j])
        j++;
      ll tmp = i - con[j - 1];
      if (tmp % 2 == 0)
        ans[con[j - 1]]++;
      else
        ans[con[j - 1] - 1]++;
    }
  }
  REP(i, len) cout << ans[i] << " ";
  cout << endl;

  return 0;
}
Problem p02954 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
long long int mod = 1e9 + 7;
bool debug = false;
typedef long long ll;
int rf(int x, string s) {
  int i = 0;
  for (i = x; i < s.size(); i++) {
    if (s[i] == 'L')
      break;
  }
  if ((i - x) % 2 == 0)
    return i;
  else
    return i - 1;
}
int lf(int x, string s) {
  int i = 0;
  for (i = x; i >= 0; i--) {
    if (s[i] == 'R')
      break;
  }
  if ((x - i) % 2 == 0)
    return i;
  else
    return i + 1;
}
int main() {
  string s;
  cin >> s;
  vector<int> p(s.size(), -1);
  vector<int> map(s.size(), 0);
  rep(i, s.size()) {
    if (p[i] == -1) {
      if (s[i] == 'R') {
        if (i >= 2) {
          if (s[i - 2] == 'R' && s[i - 1] == 'R')
            p[i] = p[i - 2];
          else
            p[i] = rf(i, s);
        } else
          p[i] = rf(i, s);
      } else {
        p[i] = lf(i, s);
        if (i < s.size() - 2) {
          if (s[i + 1] == 'L' && s[i + 2] == 'L') {
            p[i + 2] = p[i];
          }
        }
      }
    }
  }
  rep(i, s.size()) map[p[i]] += 1;
  rep(i, s.size()) {
    cout << map[i];
    if (i != s.size() - 1)
      cout << " ";
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
long long int mod = 1e9 + 7;
bool debug = false;
typedef long long ll;
int rf(int x, string s) {
  int i = 0;
  for (i = x; i < s.size(); i++) {
    if (s[i] == 'L')
      break;
  }
  if ((i - x) % 2 == 0)
    return i;
  else
    return i - 1;
}
int lf(int x, string s) {
  int i = 0;
  for (i = x; i >= 0; i--) {
    if (s[i] == 'R')
      break;
  }
  if ((x - i) % 2 == 0)
    return i;
  else
    return i + 1;
}
int main() {
  string s;
  cin >> s;
  vector<int> p(s.size(), -1);
  vector<int> map(s.size(), 0);
  rep(i, s.size()) {
    if (p[i] == -1) {
      if (s[i] == 'R') {
        if (i >= 2) {
          if (s[i - 2] == 'R' && s[i - 1] == 'R')
            p[i] = p[i - 2];
          else
            p[i] = rf(i, s);
        } else
          p[i] = rf(i, s);
      } else {
        if (i >= 2) {
          if (s[i - 2] == 'L' && s[i - 1] == 'L')
            p[i] = p[i - 2];
          else
            p[i] = lf(i, s);
        } else
          p[i] = lf(i, s);
      }
    }
  }
  rep(i, s.size()) map[p[i]] += 1;
  rep(i, s.size()) {
    cout << map[i];
    if (i != s.size() - 1)
      cout << " ";
  }
  cout << endl;
  return 0;
}
Problem p02954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  cin >> s;
  int a[s.size()];
  int ans[10005] = {0};

  int k = s.size() - 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == 'L')
      k = i;
    if (s[i] == 'R')
      a[i] = k;
  }
  k = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'R')
      k = i;
    if (s[i] == 'L')
      a[i] = k;
  }

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'R') {
      if ((a[i] - i) % 2 == 0)
        ans[a[i]]++;
      if ((a[i] - i) % 2 != 0)
        ans[a[i] - 1]++;
    }
    if (s[i] == 'L') {
      if ((i - a[i]) % 2 != 0)
        ans[a[i] + 1]++;
      if ((i - a[i]) % 2 == 0)
        ans[a[i]]++;
    }
  }

  cout << ans[0];
  for (int i = 1; i < s.size(); i++) {
    cout << ' ' << ans[i];
  }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  cin >> s;
  int a[100005] = {0};
  int ans[100005] = {0};

  int k = s.size() - 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == 'L')
      k = i;
    if (s[i] == 'R')
      a[i] = k;
  }
  k = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'R')
      k = i;
    if (s[i] == 'L')
      a[i] = k;
  }

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'R') {
      if ((a[i] - i) % 2 == 0)
        ans[a[i]]++;
      if ((a[i] - i) % 2 != 0)
        ans[a[i] - 1]++;
    }
    if (s[i] == 'L') {
      if ((i - a[i]) % 2 != 0)
        ans[a[i] + 1]++;
      if ((i - a[i]) % 2 == 0)
        ans[a[i]]++;
    }
  }

  cout << ans[0];
  for (int i = 1; i < s.size(); i++) {
    cout << ' ' << ans[i];
  }
  cout << endl;
}
Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int sum = 0;
  for (auto &x : a) {
    cin >> x;
    sum += x;
  }
  vector<int> d;
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      d.emplace_back(i);
      if (i * i < sum) {
        d.emplace_back(sum / i);
      }
    }
  }
  sort(d.begin(), d.end(), greater<int>());
  for (int x : d) {
    auto r = a;
    for (auto &y : r) {
      y %= x;
    }
    sort(r.begin(), r.end());
    vector<int> pre(n), suf(n);
    int tmp = 0;
    for (int i = 0; i < n; i++) {
      pre[i] = tmp += r[i];
    }
    tmp = 0;
    for (int i = n - 1; i >= 0; i--) {
      suf[i] = tmp += r[i];
    }
    int least = -1;
    for (int i = n - 2; i >= 0; i--) {
      if (pre[i] + suf[i + 1] == x * (n - i - 1)) {
        least = pre[i];
        break;
      }
    }
    assert(least > -1);
    if (least <= k) {
      cout << x;
      break;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int sum = 0;
  for (auto &x : a) {
    cin >> x;
    sum += x;
  }
  vector<int> d;
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      d.emplace_back(i);
      if (i * i < sum) {
        d.emplace_back(sum / i);
      }
    }
  }
  sort(d.begin(), d.end(), greater<int>());
  for (int x : d) {
    auto r = a;
    for (auto &y : r) {
      y %= x;
    }
    sort(r.begin(), r.end());
    vector<int> pre(n), suf(n);
    int tmp = 0;
    for (int i = 0; i < n; i++) {
      pre[i] = tmp += r[i];
    }
    tmp = 0;
    for (int i = n - 1; i >= 0; i--) {
      suf[i] = tmp += r[i];
    }
    int least = -1;
    int tot = suf[0];
    for (int i = n - 1; i >= 0; i--) {
      if (tot == x * (n - i - 1)) {
        least = pre[i];
        break;
      }
    }
    assert(least > -1);
    if (least <= k) {
      cout << x;
      break;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}
Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

template <typename T> istream &operator>>(istream &is, vector<T> &vs) {
  for (T &v : vs)
    is >> v;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  for (const T &v : vs)
    os << v << " ";
  return os;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  cin >> A;
  int S = accumulate(A.begin(), A.end(), 0);

  // Enumerate common divisors, and sort descent.
  vector<int> d;
  for (int i = 1; i * i <= S; ++i) {
    if (S % i == 0) {
      d.push_back(i);
      d.push_back(S / i);
    }
  }
  sort(d.rbegin(), d.rend());

  for (int x : d) {
    vector<int> d2;
    for (int v : A) {
      if (v % x != 0)
        d2.push_back(v % x);
    }
    int N2 = d2.size();
    if (N2 == 0) {
      cout << x << endl;
      return 0;
    }

    sort(d2.begin(), d2.end());

    vector<int> S1(N2 + 1), S2(N2 + 1);
    S1[0] = 0, S2[N2] = 0;
    for (int i = 0; i <= N2; ++i)
      S1[i + 1] = S1[i] + d2[i];
    for (int i = N2; i > 0; --i)
      S2[i - 1] = S2[i] + (x - d2[i - 1]);

    for (int i = 0; i < N2; ++i) {
      if (S1[i] == S2[i] && S1[i] <= K) {
        cout << x << endl;
        return 0;
      }
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

template <typename T> istream &operator>>(istream &is, vector<T> &vs) {
  for (T &v : vs)
    is >> v;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  for (const T &v : vs)
    os << v << " ";
  return os;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  cin >> A;
  int S = accumulate(A.begin(), A.end(), 0);

  // Enumerate common divisors, and sort descent.
  vector<int> d;
  for (int i = 1; i * i <= S; ++i) {
    if (S % i == 0) {
      d.push_back(i);
      d.push_back(S / i);
    }
  }
  sort(d.rbegin(), d.rend());

  for (int x : d) {
    vector<int> d2;
    for (int v : A) {
      if (v % x != 0)
        d2.push_back(v % x);
    }
    int N2 = d2.size();
    if (N2 == 0) {
      cout << x << endl;
      return 0;
    }

    sort(d2.begin(), d2.end());

    vector<int> S1(N2 + 1), S2(N2 + 1);
    S1[0] = 0, S2[N2] = 0;
    for (int i = 0; i < N2; ++i)
      S1[i + 1] = S1[i] + d2[i];
    for (int i = N2; i > 0; --i)
      S2[i - 1] = S2[i] + (x - d2[i - 1]);

    for (int i = 0; i < N2; ++i) {
      if (S1[i] == S2[i] && S1[i] <= K) {
        cout << x << endl;
        return 0;
      }
    }
  }
}
Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll sum = 0;
  rep(i, n) sum += a[i];
  set<ll> candidates;
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      candidates.insert(i);
      candidates.insert(i / sum);
    }
  }
  ll ans = 1;
  for (ll x : candidates) {
    ll need;
    {
      vector<ll> r(n);
      rep(i, n) r[i] = a[i] % x;
      sort(r.begin(), r.end());
      ll B = 0;
      rep(i, n) B += x - r[i];
      ll A = 0;
      need = 1e18;
      rep(i, n) {
        A += r[i];
        B -= x - r[i];
        need = min(need, max(A, B));
      }
    }
    if (need <= k)
      ans = max(ans, x);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll sum = 0;
  rep(i, n) sum += a[i];
  set<ll> candidates;
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      candidates.insert(i);
      candidates.insert(sum / i);
    }
  }
  ll ans = 1;
  for (ll x : candidates) {
    ll need;
    {
      vector<ll> r(n);
      rep(i, n) r[i] = a[i] % x;
      sort(r.begin(), r.end());
      ll B = 0;
      rep(i, n) B += x - r[i];
      ll A = 0;
      need = 1e18;
      rep(i, n) {
        A += r[i];
        B -= x - r[i];
        need = min(need, max(A, B));
      }
    }
    if (need <= k)
      ans = max(ans, x);
  }
  cout << ans << endl;

  return 0;
}
Problem p02955 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define MOD (int)1e9 + 7

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll s = 0;
  rep(i, n) {
    cin >> a[i];
    s += a[i];
  }

  vector<ll> mod;
  for (ll i = s; i >= sqrt(s); i--) {
    if (s % i == 0) {
      mod.emplace_back(i);
      mod.emplace_back(s / i);
    }
  }
  sort(mod.begin(), mod.end(), greater<>());

  for (int i = 0; i < mod.size(); i++) {
    ll m = mod[i];
    ll cnt = 0;
    vector<ll> amari(n);
    rep(j, n) { amari[j] = a[j] % m; }
    sort(amari.begin(), amari.end());
    ll l = 0, r = n - 1;
    while (l < r) {
      if (amari[l] < m - amari[r]) {
        cnt += amari[l];
        amari[r] += amari[l];
        amari[l] = 0;
        l++;
      } else {
        cnt += (m - amari[r]);
        amari[l] -= (m - amari[r]);
        amari[r] = m;
        r--;
      }
    }
    if (cnt <= k) {
      cout << m << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define MOD (int)1e9 + 7

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll s = 0;
  rep(i, n) {
    cin >> a[i];
    s += a[i];
  }

  vector<ll> mod;
  for (ll i = 1; i <= sqrt(s); i++) {
    if (s % i == 0) {
      mod.emplace_back(i);
      mod.emplace_back(s / i);
    }
  }
  sort(mod.begin(), mod.end(), greater<>());

  for (int i = 0; i < mod.size(); i++) {
    ll m = mod[i];
    ll cnt = 0;
    vector<ll> amari(n);
    rep(j, n) { amari[j] = a[j] % m; }
    sort(amari.begin(), amari.end());
    ll l = 0, r = n - 1;
    while (l < r) {
      if (amari[l] < m - amari[r]) {
        cnt += amari[l];
        amari[r] += amari[l];
        amari[l] = 0;
        l++;
      } else {
        cnt += (m - amari[r]);
        amari[l] -= (m - amari[r]);
        amari[r] = m;
        r--;
      }
    }
    if (cnt <= k) {
      cout << m << endl;
      return 0;
    }
  }
}
Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x.size())
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define uniq(x) x.erase(unique(x.begin(), x.end()), x.end())
#define F first
#define S second
#define mp make_pair
#define eb emplace_back
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

typedef long long ll;

/* --- INFの値が適切か確認する --- */
// const int INF = 1 << 30;
// const ll INF = 1LL << 60;
// const int MOD = 1000000007;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  int sum = 0;
  rep(i, N) sum += A[i];

  vector<int> d;
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      d.eb(i);
      if (sum / i != i) {
        d.eb(sum / i);
      }
    }
  }

  sort(rall(d));

  rep(i, sz(d)) {
    int mod = d[i];
    vector<int> a = A;
    rep(j, sz(a)) a[i] %= mod;

    sort(all(a));
    ll m = 0, p = 0;
    rep(j, sz(a)) p += (mod - a[j]);

    rep(j, sz(a)) {
      m += a[j];
      p -= (mod - a[j]);
      if (m == p && m <= K) {
        cout << mod << endl;
        return 0;
      }
    }
  }

  cout << "-1" << endl;
}


Fixed submission:
#include <bits/stdc++.h>

#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x.size())
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define uniq(x) x.erase(unique(x.begin(), x.end()), x.end())
#define F first
#define S second
#define mp make_pair
#define eb emplace_back
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

typedef long long ll;

/* --- INFの値が適切か確認する --- */
// const int INF = 1 << 30;
// const ll INF = 1LL << 60;
// const int MOD = 1000000007;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  int sum = 0;
  rep(i, N) sum += A[i];

  vector<int> d;
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      d.eb(i);
      if (sum / i != i) {
        d.eb(sum / i);
      }
    }
  }

  sort(rall(d));

  rep(i, sz(d)) {
    int mod = d[i];
    vector<int> a = A;
    rep(j, sz(a)) a[j] %= mod;

    sort(all(a));
    ll m = 0, p = 0;
    rep(j, sz(a)) p += (mod - a[j]);

    rep(j, sz(a)) {
      m += a[j];
      p -= (mod - a[j]);
      if (m == p && m <= K) {
        cout << mod << endl;
        return 0;
      }
    }
  }

  cout << "-1" << endl;
}
Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long int ll;
typedef pair<int, int> P;

int n, k, a[1000];

int main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  int sum = 0;
  rep(i, n) sum += a[i];

  vector<int> g(0);
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      g.emplace_back(i);
      g.emplace_back(sum / i);
    }
  }
  sort(g.begin(), g.end(), greater<int>());

  int res = 0;
  for (auto x : g) {
    int b[n];
    rep(i, n) b[n] = a[n] % x;
    sort(b, b + n, greater<int>());
    int sum = 0;
    rep(i, n) sum += b[i];
    int tmp = 0;
    rep(i, sum / x) tmp += b[i];
    sum -= tmp;
    if (sum <= k) {
      cout << x << endl;
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long int ll;
typedef pair<int, int> P;

int n, k, a[1000];

int main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  int sum = 0;
  rep(i, n) sum += a[i];

  vector<int> g(0);
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      g.emplace_back(i);
      g.emplace_back(sum / i);
    }
  }
  sort(g.begin(), g.end(), greater<int>());

  int res = 0;
  for (auto x : g) {
    int b[n];
    rep(i, n) b[i] = a[i] % x;
    sort(b, b + n, greater<int>());
    int sum = 0;
    rep(i, n) sum += b[i];
    int tmp = 0;
    rep(i, sum / x) tmp += b[i];
    sum -= tmp;
    if (sum <= k) {
      cout << x << endl;
      break;
    }
  }
  return 0;
}
Problem p02955 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int infI = 1e9 + 5;
const long long infL = 1e18;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;

#define mp make_pair
#define pb push_back
#define ii pair<int, int>
#define ll long long
#define pll pair<ll, ll>
#define vl vector<vector<ll>>
#define vi vector<vector<int>>
#define eb emplace_back
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define endl '\n'
#define popb pop_back();
#define se second
#define fi first

int n, k;
int a[505];

int main() {
  fastio;
  cin >> n >> k;
  ll sum = 0;
  forn(i, 0, n) {
    cin >> a[i];
    sum += a[i];
  }
  vector<int> f;
  forn(i, 1, sqrt(sum)) {
    f.pb(i);
    f.pb(sum / i);
  }
  ll ans = 0;
  forn(i, 0, f.size()) {
    int a1 = f[i];
    ll total = 0, used = 0;
    multiset<int> v;
    forn(i, 0, n) {
      v.insert(a[i] % a1);
      total += (a1 - a[i] % a1);
    }
    for (auto itr = v.begin(); itr != v.end(); ++itr) {
      used += *itr;
      total -= (a1 - *itr);
      if (used == total && used <= k) {
        ans = max(ans, (ll)a1);
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int infI = 1e9 + 5;
const long long infL = 1e18;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;

#define mp make_pair
#define pb push_back
#define ii pair<int, int>
#define ll long long
#define pll pair<ll, ll>
#define vl vector<vector<ll>>
#define vi vector<vector<int>>
#define eb emplace_back
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define endl '\n'
#define popb pop_back();
#define se second
#define fi first

int n, k;
int a[505];

int main() {
  fastio;
  cin >> n >> k;
  ll sum = 0;
  forn(i, 0, n) {
    cin >> a[i];
    sum += a[i];
  }
  vector<int> f;
  forn(i, 1, sqrt(sum)) {
    if (sum % i == 0) {
      f.pb(i);
      f.pb(sum / i);
    }
  }
  ll ans = 0;
  forn(i, 0, f.size()) {
    int a1 = f[i];
    ll total = 0, used = 0;
    multiset<int> v;
    forn(i, 0, n) {
      v.insert(a[i] % a1);
      total += (a1 - a[i] % a1);
    }
    for (auto itr = v.begin(); itr != v.end(); ++itr) {
      used += *itr;
      total -= (a1 - *itr);
      if (used == total && used <= k) {
        ans = max(ans, (ll)a1);
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 505
#define int long long
using namespace std;
int n, m, a[N], p[N], tot, cnt, K, L[N], R[N], M = 10000000;
bool check(int x) {
  memset(L, 0, sizeof(L));
  memset(R, 0, sizeof(R));
  for (int i = 1; i <= n; i++) {
    int t = a[i] % x;
    L[i] = t;
  }
  sort(L + 1, L + n + 1);
  R[n] = x - L[n];
  for (int i = n - 1; i >= 1; i--)
    R[i] = R[i + 1] + x - L[i];
  for (int i = 2; i <= n; i++)
    L[i] += L[i - 1];
  int res = 1e9;
  for (int i = 1; i <= n; i++)
    res = min(res, max(L[i], R[i + 1]));
  return res <= K;
}
signed main() {
  scanf("%lld%lld", &n, &K);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), m += a[i];
  for (int i = 1; 1ll * i * i <= 1ll * m; i++)
    if (m % i == 0) {
      p[++tot] = i;
      if (1ll * i * i != 1ll * m)
        p[++tot] = m / i;
    }
  sort(p + 1, p + tot + 1);
  for (int i = tot; i >= 1; i--) {
    if (check(p[i])) {
      printf("%lld\n", p[i]);
      return 0;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 505
#define int long long
using namespace std;
int n, m, a[N], p[N * N], tot, cnt, K, L[N], R[N];
bool check(int x) {
  memset(L, 0, sizeof(L));
  memset(R, 0, sizeof(R));
  for (int i = 1; i <= n; i++) {
    int t = a[i] % x;
    L[i] = t;
  }
  sort(L + 1, L + n + 1);
  R[n] = x - L[n];
  for (int i = n - 1; i >= 1; i--)
    R[i] = R[i + 1] + x - L[i];
  for (int i = 2; i <= n; i++)
    L[i] += L[i - 1];
  int res = 1e9;
  for (int i = 1; i <= n; i++)
    res = min(res, max(L[i], R[i + 1]));
  return res <= K;
}
signed main() {
  scanf("%lld%lld", &n, &K);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), m += a[i];
  for (int i = 1; 1ll * i * i <= 1ll * m; i++)
    if (m % i == 0) {
      p[++tot] = i;
      if (1ll * i * i != 1ll * m)
        p[++tot] = m / i;
    }
  sort(p + 1, p + tot + 1);
  for (int i = tot; i >= 1; i--) {
    if (check(p[i])) {
      printf("%lld\n", p[i]);
      return 0;
    }
  }
  return 0;
}
Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>

using namespace std;

vector<long long> div(long long n) {
  vector<long long> res;
  for (long long i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      res.emplace_back(i);
      if (i != n / i)
        res.emplace_back(n / i);
    }
  }
  return res;
}

int main() {

  long long n, k;

  cin >> n >> k;

  vector<long long> a(n);

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<long long> divs = div(accumulate(a.begin(), a.end(), 0LL));

  long long ans = 0;

  for (auto &e : divs) {

    vector<long long> mods(n);
    for (int i = 0; i < n; i++)
      mods[i] = a[i] % e;

    sort(mods.begin(), mods.end());

    vector<long long> minus(n + 1);
    for (int i = 0; i < n; i++)
      minus[i + 1] = minus[i] + mods[i];

    vector<long long> plus(n + 1);
    for (int i = n - 1; i >= 0; i++)
      plus[i] = plus[i + 1] + e - mods[i];

    for (long long i = 0; i <= n; ++i) {
      if (max(plus[i], minus[i]) <= k)
        ans = max(ans, e);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>

using namespace std;

vector<long long> div(long long n) {
  vector<long long> res;
  for (long long i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      res.emplace_back(i);
      if (i != n / i)
        res.emplace_back(n / i);
    }
  }
  return res;
}

int main() {

  long long n, k;

  cin >> n >> k;

  vector<long long> a(n);

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<long long> divs = div(accumulate(a.begin(), a.end(), 0LL));

  long long ans = 0;

  for (auto &e : divs) {

    vector<long long> mods(n);
    for (int i = 0; i < n; i++)
      mods[i] = a[i] % e;

    sort(mods.begin(), mods.end());

    vector<long long> minus(n + 1);
    for (int i = 0; i < n; i++)
      minus[i + 1] = minus[i] + mods[i];

    vector<long long> plus(n + 1);
    for (int i = n - 1; i >= 0; i--)
      plus[i] = plus[i + 1] + e - mods[i];

    for (long long i = 0; i <= n; ++i) {
      if (max(plus[i], minus[i]) <= k)
        ans = max(ans, e);
    }
  }
  cout << ans << endl;
}
Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define len(v) ll(v.size())
#define fi first
#define se second

template <class T> void cout_vec(const vector<T> &vec) {
  for (auto itr : vec)
    cout << itr << ' ';
  cout << '\n';
}

typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;

template <class T> vector<T> divisor(T x) {
  vector<T> res;
  for (T i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      res.push_back(i);
      if (i != x / i) {
        res.push_back(x / i);
      }
    }
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  vector<ll> ret = divisor(sum);
  sort(begin(ret), end(ret), greater<ll>());
  rep(i, len(ret)) {
    ll now = ret[i];
    vector<ll> cnt;
    rep(j, n) {
      if (a[j] % now == 0)
        continue;
      cnt.push_back(a[j] % now);
    }
    sort(begin(cnt), end(cnt));
    ll m = len(cnt);
    // cout<<now<<endl;
    vector<ll> l(m, 0);
    l[0] = cnt[0];
    FOR(j, 1, m) { l[j] = l[j - 1] + cnt[j]; }
    // cout_vec(l);
    rep(j, m) {
      if (l[j] == (now * (m - 1 - j) - (l[m - 1] - l[j])) && l[j] <= k) {
        cout << now << endl;
        return 0;
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define len(v) ll(v.size())
#define fi first
#define se second

template <class T> void cout_vec(const vector<T> &vec) {
  for (auto itr : vec)
    cout << itr << ' ';
  cout << '\n';
}

typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;

template <class T> vector<T> divisor(T x) {
  vector<T> res;
  for (T i = 1; i * i <= x; i++) {
    if (x % i == 0) {
      res.push_back(i);
      if (i != x / i) {
        res.push_back(x / i);
      }
    }
  }
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  vector<ll> ret = divisor(sum);
  sort(begin(ret), end(ret), greater<ll>());
  rep(i, len(ret)) {
    ll now = ret[i];
    if (now == 1) {
      cout << now << endl;
      return 0;
    }
    vector<ll> cnt;
    rep(j, n) {
      if (a[j] % now == 0)
        continue;
      cnt.push_back(a[j] % now);
    }
    sort(begin(cnt), end(cnt));
    ll m = len(cnt);
    // cout<<now<<endl;
    vector<ll> l(m, 0);
    l[0] = cnt[0];
    FOR(j, 1, m) { l[j] = l[j - 1] + cnt[j]; }
    // cout_vec(l);
    rep(j, m) {
      if (l[j] == (now * (m - 1 - j) - (l[m - 1] - l[j])) && l[j] <= k) {
        cout << now << endl;
        return 0;
      }
    }
  }
}
Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.rbegin(), A.rend());

  auto minOp = [&](ll x) {
    // Aを全てxで割り切れるようにするときにかかる最小回数
    vector<ll> mod(N);
    for (ll i = 0; i < N; i++) {
      mod[i] = A[i] % x;
    }
    sort(mod.begin(), mod.end());
    ll back = N - 1;
    ll num = 0;
    for (ll i = 0; i < N; i++) {
      // mod[i]は減らす,mod[back]は増やす
      if (mod[i] == x - mod[back]) {
        ll curr_op_num = mod[i];
        num += curr_op_num;
        mod[i] -= curr_op_num;
        mod[back] += curr_op_num;
        back--;
      } else if (mod[i] > x - mod[back]) {
        ll curr_op_num = x - mod[back];
        num += curr_op_num;
        mod[i] -= curr_op_num;
        mod[back] += curr_op_num;
        back--;
        i--;
      } else {
        ll curr_op_num = mod[i];
        num += curr_op_num;
        mod[i] -= curr_op_num;
        mod[back] += curr_op_num;
      }
    }
    for (ll i = 0; i < N; i++) {
      assert(mod[i] % x == 0);
    }

    return num;
  };

  ll sum = accumulate(A.begin(), A.end(), (ll)0);

  ll ans = 0;
  for (ll i = 1; i * i <= sum; i++) {
    if (sum % i != 0) {
      continue;
    }

    if (minOp(sum / i) <= K) {
      ans = max(ans, sum / i);
      continue;
    }
    if (minOp(i) <= K) {
      ans = max(ans, i);
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(A.rbegin(), A.rend());

  auto minOp = [&](ll x) {
    // Aを全てxで割り切れるようにするときにかかる最小回数
    vector<ll> mod(N);
    for (ll i = 0; i < N; i++) {
      mod[i] = A[i] % x;
    }
    sort(mod.begin(), mod.end());
    ll back = N - 1;
    ll num = 0;
    for (ll i = 0; i < back; i++) {
      // mod[i]は減らす,mod[back]は増やす
      if (mod[i] == x - mod[back]) {
        ll curr_op_num = mod[i];
        num += curr_op_num;
        mod[i] -= curr_op_num;
        mod[back] += curr_op_num;
        back--;
      } else if (mod[i] > x - mod[back]) {
        ll curr_op_num = x - mod[back];
        num += curr_op_num;
        mod[i] -= curr_op_num;
        mod[back] += curr_op_num;
        back--;
        i--;
      } else {
        ll curr_op_num = mod[i];
        num += curr_op_num;
        mod[i] -= curr_op_num;
        mod[back] += curr_op_num;
      }
    }
    for (ll i = 0; i < N; i++) {
      assert(mod[i] % x == 0);
    }

    return num;
  };

  ll sum = accumulate(A.begin(), A.end(), (ll)0);

  ll ans = 0;
  for (ll i = 1; i * i <= sum; i++) {
    if (sum % i != 0) {
      continue;
    }

    if (minOp(sum / i) <= K) {
      ans = max(ans, sum / i);
      continue;
    }
    if (minOp(i) <= K) {
      ans = max(ans, i);
    }
  }

  cout << ans << endl;
}
Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n;
  cin >> k;
  int a[n];
  int asum;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    asum += a[i];
  }

  int divisor[asum];
  int cursor = 0;

  for (int i = 1; i * i <= asum; i++) {
    if (asum % i == 0) {
      divisor[cursor] = i;
      if (i * i != asum)
        divisor[cursor + 1] = asum / i;
      cursor += 2;
    }
  }

  int divisorlen = cursor;
  sort(divisor, divisor + divisorlen, greater<int>());

  int rem[n];
  int r;
  int sum, nsum;

  for (int i = 0; i < divisorlen; i++) {
    int d = divisor[i];
    sum = 0;
    nsum = 0;
    for (int i = 0; i < n; i++) {
      r = a[i] % d;
      sum += r;
      rem[i] = r;
    }

    sort(rem, rem + n, greater<int>());

    for (int i = 0; i < n; i++) {
      if (sum == nsum) {
        if (sum <= k) {
          cout << d << "\n";
          return 0;
        } else {
          continue;
        }
      }
      sum -= rem[i];
      nsum += d - rem[i];
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n;
  cin >> k;
  int a[n];
  int asum;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    asum += a[i];
  }

  int divisor[30000];
  int cursor = 0;

  for (int i = 1; i * i <= asum; i++) {
    if (asum % i == 0) {
      divisor[cursor] = i;
      if (i * i != asum)
        divisor[cursor + 1] = asum / i;
      cursor += 2;
    }
  }

  int divisorlen = cursor;
  sort(divisor, divisor + divisorlen, greater<int>());

  int rem[n];
  int r;
  int sum, nsum;

  for (int i = 0; i < divisorlen; i++) {
    int d = divisor[i];
    sum = 0;
    nsum = 0;
    for (int i = 0; i < n; i++) {
      r = a[i] % d;
      sum += r;
      rem[i] = r;
    }

    sort(rem, rem + n, greater<int>());

    for (int i = 0; i < n; i++) {
      if (sum == nsum) {
        if (sum <= k) {
          cout << d << "\n";
          return 0;
        } else {
          continue;
        }
      }
      sum -= rem[i];
      nsum += d - rem[i];
    }
  }
  return 0;
}