Problem p02955 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;

// LLONG_MAX
#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v, a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v, a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
using namespace std;

int N, K;
int A[501];

int func(int n) {
  int a[501];
  for (int i = 0; i < N; i++) {
    a[i] = A[i] % n;
  }
  ARY_SORT(a, N);

  int cnt = 0;
  int end = N - 1;
  for (int i = 0; i <= end; i++) {
    int end_rest = n - a[end];

    if (a[i] == end_rest) {
      cnt += a[i];

      a[end] += a[i];
      a[i] = 0;
      end--;
    } else if (a[i] < end_rest) {
      cnt += a[i];

      a[end] += a[i];
      a[i] = 0;
    } else if (a[i] > end_rest) {
      int sub = n - a[end];
      cnt += sub;
      a[i] -= sub;
      a[end] += sub;
      end--;
      i--;
    }
  }

  return cnt;
}

signed main() {
  cin >> N >> K;

  int sum = 0;
  REP(i, N) {
    cin >> A[i];
    sum += A[i];
  }

  int num = sum;
  int max = 0;
  for (int i = 1; i <= num; i++) {
    if (sum % i == 0) {
      num = sum / i;

      int a = func(i);
      int b = func(num);

      if (a <= K)
        max = MAX(max, i);
      if (b <= K)
        max = MAX(max, num);
    }
  }

  cout << max << endl;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;

// LLONG_MAX
#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v, a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v, a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
using namespace std;

int N, K;
int A[501];

int func(int n) {
  int a[501];
  for (int i = 0; i < N; i++) {
    a[i] = A[i] % n;
  }
  ARY_SORT(a, N);

  int cnt = 0;
  int end = N - 1;
  for (int i = 0; i <= end; i++) {
    int end_rest = n - a[end];

    if (a[i] == end_rest) {
      cnt += a[i];

      a[end] += a[i];
      a[i] = 0;
      end--;
    } else if (a[i] < end_rest) {
      cnt += a[i];

      a[end] += a[i];
      a[i] = 0;
    } else if (a[i] > end_rest) {
      int sub = n - a[end];
      cnt += sub;
      a[i] -= sub;
      a[end] += sub;
      end--;
      i--;
    }
  }

  return cnt;
}

signed main() {
  cin >> N >> K;

  int sum = 0;
  REP(i, N) {
    cin >> A[i];
    sum += A[i];
  }

  int num = sum;
  int max = 0;
  for (int i = 1; i <= num; i++) {
    if (sum % i == 0) {
      num = sum / i;

      int a = func(i);
      int b = func(num);

      if (a <= K)
        max = MAX(max, i);
      if (b <= K) {
        cout << num << endl;
        return 0;
      }
    }
  }

  cout << max << endl;
}