Problem p02955 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int read() {
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
long long READ() {
  long long xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
char one() {
  char ch = getchar();
  while (ch == ' ' || ch == '\n')
    ch = getchar();
  return ch;
}
inline long long mymin(long long A, long long B) {
  if (A < B)
    return A;
  return B;
}
inline long long mymax(long long A, long long B) {
  if (A > B)
    return A;
  return B;
}
const int maxn = 510;
int N, K, a[maxn], sum;
int p[maxn], cnt;
long long b[maxn];
void div(int x) {
  for (int i = 1; i * i <= x; i++)
    if (x % i == 0) {
      p[++cnt] = i;
      if (i * i != x)
        p[++cnt] = x / i;
    }
}
bool check(int x) {
  for (int i = 1; i <= N; i++)
    b[i] = a[i] % x;
  sort(b + 1, b + 1 + N);
  for (int i = 1; i <= N; i++)
    b[i] += b[i - 1];
  long long ret = K + 1;
  for (int i = 1; i <= N; i++)
    ret = mymin(ret, mymax(b[i], 1LL * (N - i) * x - (b[N] - b[i])));
  return ret <= K;
}
int main() {
  // freopen("in","r",stdin);
  N = read(), K = read();
  for (int i = 1; i <= N; i++)
    a[i] = read(), sum += a[i];
  div(sum);
  int ans = 1;
  for (int i = 1; i <= cnt; i++)
    if (p[i] > ans && check(p[i]))
      ans = max(ans, p[i]);
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int read() {
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
long long READ() {
  long long xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
char one() {
  char ch = getchar();
  while (ch == ' ' || ch == '\n')
    ch = getchar();
  return ch;
}
inline long long mymin(long long A, long long B) {
  if (A < B)
    return A;
  return B;
}
inline long long mymax(long long A, long long B) {
  if (A > B)
    return A;
  return B;
}
const int maxn = 510;
int N, K, a[maxn], sum;
int p[10010], cnt;
long long b[maxn];
void div(int x) {
  for (int i = 1; i * i <= x; i++)
    if (x % i == 0) {
      p[++cnt] = i;
      if (i * i != x)
        p[++cnt] = x / i;
    }
}
bool check(int x) {
  for (int i = 1; i <= N; i++)
    b[i] = a[i] % x;
  sort(b + 1, b + 1 + N);
  for (int i = 1; i <= N; i++)
    b[i] += b[i - 1];
  long long ret = K + 1;
  for (int i = 1; i <= N; i++)
    ret = mymin(ret, mymax(b[i], 1LL * (N - i) * x - (b[N] - b[i])));
  return ret <= K;
}
int main() {
  // freopen("in","r",stdin);
  N = read(), K = read();
  for (int i = 1; i <= N; i++)
    a[i] = read(), sum += a[i];
  div(sum);
  int ans = 1;
  for (int i = 1; i <= cnt; i++)
    if (p[i] > ans && check(p[i]))
      ans = max(ans, p[i]);
  printf("%d\n", ans);
  return 0;
}