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

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
bool a[20010], b[20010];
int main() {
  int n, m;
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < n + 1; i++) {
    a[i] = false;
    b[i] = false;
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    if (x == 1)
      b[y] = true;
    if (x == n)
      a[y] == true;
    if (y == 1)
      b[x] == true;
    if (y == n)
      a[x] = true;
  }
  for (int i = 1; i <= n; i++)
    if (a[i] && b[i]) {
      cout << "POSSIBLE" << endl;
      return 0;
    }
  cout << "IMPOSSIBLE" << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
bool a[200010], b[200010];
int main() {
  int n, m;
  cin >> n >> m;
  int x, y;
  for (int i = 0; i < n + 1; i++) {
    a[i] = false;
    b[i] = false;
  }
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    if (x == 1)
      b[y] = true;
    if (x == n)
      a[y] == true;
    if (y == 1)
      b[x] == true;
    if (y == n)
      a[x] = true;
  }
  for (int i = 1; i <= n; i++)
    if (a[i] && b[i]) {
      cout << "POSSIBLE" << endl;
      return 0;
    }
  cout << "IMPOSSIBLE" << endl;
  return 0;
}
Problem p03648 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define FOR(j, k, n) for (int j = k; j < (int)(n); j++)
#define MAXN 100000000000000000
#define newl printf("\n")
#define test(t)                                                                \
  int t;                                                                       \
  cin >> t;                                                                    \
  for (int tes = 0; tes < t; tes++)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
typedef long long int lli;
const int MM = 220000;
lli m, n, k, p1, p2, p3, p, f, a[MM];
string s, s1, s2;
set<lli> g[MM];
void doi(lli x) {
  lli u = n % x;

  cout << x << endl;
  lli alf = (n / x) + x - 1;
  for (int i = 0; i < u; i++)
    cout << alf + x - (u - 1) << " ";

  for (int i = u; i < x; i++) {
    cout << alf - u << " ";
  }
}
lli mm(lli x) { return (n / x) + x - 1 + x * (n % x); }
int main() {
  fio;
  // Have you solved the problem completely?
  cin >> n;
  lli mn = 10000000000001000;
  for (lli i = 50; i >= 1; i--) {
    if (mm(i) <= mn) {
      doi(i);
      return 0;
    }
  }
  assert(false);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define FOR(j, k, n) for (int j = k; j < (int)(n); j++)
#define MAXN 100000000000000000
#define newl printf("\n")
#define test(t)                                                                \
  int t;                                                                       \
  cin >> t;                                                                    \
  for (int tes = 0; tes < t; tes++)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
typedef long long int lli;
const int MM = 220000;
lli m, n, k, p1, p2, p3, p, f, a[MM];
string s, s1, s2;
set<lli> g[MM];
void doi(lli x) {
  lli u = n % x;

  cout << x << endl;
  lli alf = (n / x) + x - 1;
  for (int i = 0; i < u; i++)
    cout << alf + x - (u - 1) << " ";

  for (int i = u; i < x; i++) {
    cout << alf - u << " ";
  }
}
lli mm(lli x) { return 1; }
int main() {
  fio;
  // Have you solved the problem completely?
  cin >> n;
  lli mn = 10000000000001000;
  for (lli i = 50; i >= 1; i--) {
    if (mm(i) <= mn) {
      doi(i);
      return 0;
    }
  }
  assert(false);
  return 0;
}
Problem p03648 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define int long long

const int N = 51;

const int INF = 1e16;

const int inf = INF / 51;

int n, m, k, a[51], b[51];

void read() { scanf("%lld", &k); }

int times = 0, ans = 0;

signed main() {
  read();
  if (k <= 49) {
    puts("50");
    cout << 50 * k << " ";
    for (int i = 2; i <= 50; i++)
      cout << 0 << " ";
    return 0;
  }
  n = 50;
  cout << 50 << endl;
  for (int j = 1; j <= n; j++)
    a[j] = n - 1 - k;
  for (int i = 1; i <= n; i++) {
    if (a[i] < 0) {
      int p = -a[i];
      p /= (n + 1);
      if ((n + 1) * p != -a[i]) {
        p++;
      }
      a[i] += p * (n + 1);
      k -= p;
    }
  }
  int pointer = 0;
  for (int i = 1; i <= k; i++) {
    if (pointer == 51)
      pointer = 1;
    pointer++;
    a[i] += 51;
  }
  for (int i = 1; i <= n; i++)
    cout << a[i] << " ";
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define int long long

const int N = 51;

const int INF = 1e16;

const int inf = INF / 51;

int n, m, k, a[51], b[51];

void read() { scanf("%lld", &k); }

int times = 0, ans = 0;

signed main() {
  read();
  if (k <= 49) {
    puts("50");
    cout << 50 * k << " ";
    for (int i = 2; i <= 50; i++)
      cout << 0 << " ";
    return 0;
  }
  n = 50;
  cout << 50 << endl;
  for (int j = 1; j <= n; j++)
    a[j] = n - 1 - k;
  for (int i = 1; i <= n; i++) {
    if (a[i] < 0) {
      int p = -a[i];
      p /= (n + 1);
      if ((n + 1) * p != -a[i]) {
        p++;
      }
      a[i] += p * (n + 1);
      k -= p;
    }
  }
  int cal = k / n;
  for (int i = 1; i <= n; i++) {
    a[i] += cal * (n + 1);
  }
  k %= n;
  for (int i = 1; i <= n; i++) {
    if (!k)
      break;
    a[i] += n + 1;
    k--;
  }
  for (int i = 1; i <= n; i++)
    cout << a[i] << " ";
}
Problem p03648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, a, b) for (int i = a, i##E = b; i <= i##E; ++i)
#define rFor(i, a, b) for (int i = a, i##E = b; i >= i##E; --i)
using namespace std;
typedef long long LL;
template <typename T> inline T chkmax(T A, T B) { return A > B ? A : B; }
template <typename T> inline T chkmin(T A, T B) { return A < B ? A : B; }
template <typename T> inline void read(T &x) {
  x = 0;
  int _f(0);
  char ch = getchar();
  while (!isdigit(ch))
    _f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x = _f ? -x : x;
}
inline void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
LL a[60], n, k, m;
int main() {
  n = 50;
  read(k);
  For(i, 1, 50) a[i] = (k / n) + n - 1 + (k % n ? 1 : 0);
  m = n - (k % n ? k % n : k);
  while (m--) {
    LL Max = 0, pos = 0;
    For(i, 1, n) if (a[i] > Max) Max = a[i], pos = i;
    For(i, 1, n) if (i != pos) a[i]++;
    a[pos] -= n;
  }
  printf("%lld\n", n);
  For(i, 1, n) printf("%lld ", a[i]);
  puts("");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, a, b) for (int i = a, i##E = b; i <= i##E; ++i)
#define rFor(i, a, b) for (int i = a, i##E = b; i >= i##E; --i)
using namespace std;
typedef long long LL;
template <typename T> inline T chkmax(T A, T B) { return A > B ? A : B; }
template <typename T> inline T chkmin(T A, T B) { return A < B ? A : B; }
template <typename T> inline void read(T &x) {
  x = 0;
  int _f(0);
  char ch = getchar();
  while (!isdigit(ch))
    _f |= (ch == '-'), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x = _f ? -x : x;
}
inline void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
LL a[60], n, k, m;
int main() {
  n = 50;
  read(k);
  For(i, 1, 50) a[i] = (k / n) + n - 1 + (k % n ? 1 : 0);
  m = n - (k % n ? k % n : n);
  while (m--) {
    LL Max = 0, pos = 0;
    For(i, 1, n) if (a[i] > Max) Max = a[i], pos = i;
    For(i, 1, n) if (i != pos) a[i]++;
    a[pos] -= n;
  }
  printf("%lld\n", n);
  For(i, 1, n) printf("%lld ", a[i]);
  puts("");
  return 0;
}
Problem p03648 (C++)

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

using ll = long long;

ll test(vector<ll> A) {
  int res = 0;
  int N = int(A.size());
  while (true) {
    pair<ll, int> best(-1ll, -1);
    for (int i = 0; i < N; i++) {
      best = max(best, pair<ll, int>(A[i], i));
    }
    if (best.first < N)
      break;
    for (int i = 0; i < N; i++) {
      if (best.second == i) {
        A[i] -= N;
      } else {
        A[i] += 1;
      }
    }
    res++;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ll K;
  cin >> K;
  const int N = 50;
  vector<ll> A(N);
  ll cnt = K / N;
  int rem = K - cnt * N;
  assert(cnt * N + rem == K);
  for (int i = 0; i < N; i++) {
    A[i] = i + N * cnt - (N - 1) * cnt;
  }
  for (int i = 0; i < rem; i++) {
    A[i] += N;
    for (int j = 0; j < N; j++) {
      if (i != j) {
        A[j] -= 1;
      }
    }
  }
  assert(test(A) == K);

  cout << N << '\n';
  for (int i = 0; i < N; i++) {
    cout << A[i] << " \n"[i == N - 1];
  }

  return 0;
}

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

using ll = long long;

ll test(vector<ll> A) {
  int res = 0;
  int N = int(A.size());
  while (true) {
    pair<ll, int> best(-1ll, -1);
    for (int i = 0; i < N; i++) {
      best = max(best, pair<ll, int>(A[i], i));
    }
    if (best.first < N)
      break;
    for (int i = 0; i < N; i++) {
      if (best.second == i) {
        A[i] -= N;
      } else {
        A[i] += 1;
      }
    }
    res++;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ll K;
  cin >> K;
  const int N = 50;
  vector<ll> A(N);
  ll cnt = K / N;
  int rem = K - cnt * N;
  assert(cnt * N + rem == K);
  for (int i = 0; i < N; i++) {
    A[i] = i + N * cnt - (N - 1) * cnt;
  }
  for (int i = 0; i < rem; i++) {
    A[i] += N;
    for (int j = 0; j < N; j++) {
      if (i != j) {
        A[j] -= 1;
      }
    }
  }
  // assert(test(A) == K);

  cout << N << '\n';
  for (int i = 0; i < N; i++) {
    cout << A[i] << " \n"[i == N - 1];
  }

  return 0;
}
Problem p03648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 20;
const ll inf = 1e16 + 1000;
ll k, n, a[N];
int main() {
  while (cin >> k) {
    n = min(k, 50ll);
    ll num = k / n, re = k % n;
    for (int i = 1; i <= n; i++)
      a[i] = num + i - 1;
    for (int p = 1; p <= re; p++) {
      for (int i = 1; i <= n; i++) {
        if (i == p)
          a[i] += n;
        else
          a[i]--;
      }
    }
    printf("%lld\n", n);
    for (ll i = 1; i <= n; i++)
      printf("%lld%c", a[i], i == n ? '\n' : ' ');
  }
  return 0;
}
// 25720164523
// 154320986863
// 500000000000000000

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 20;
const ll inf = 1e16 + 1000;
ll k, n, a[N];
int main() {
  while (cin >> k) {

    n = 50;
    ll num = k / n, re = k % n;
    for (int i = 1; i <= n; i++)
      a[i] = num + i - 1;
    for (int p = 1; p <= re; p++) {
      for (int i = 1; i <= n; i++) {
        if (i == p)
          a[i] += n;
        else
          a[i]--;
      }
    }
    printf("%lld\n", n);
    for (ll i = 1; i <= n; i++)
      printf("%lld%c", a[i], i == n ? '\n' : ' ');
  }
  return 0;
}
// 25720164523
// 154320986863
// 500000000000000000
Problem p03648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define MA(i, j) make_pair(i, j)
#define PA pair<int, int>
#define PB push_back
#define PQ priority_queue<int>
#define PGQ priority_queue<int, vector<int>, greater<int>>
#define VE vector<int>
#define VP vector<PA>
#define YES(i) cout << (i ? "YES" : "NO") << endl
#define Yes(i) cout << (i ? "Yes" : "No") << endl
#define MOD 1000000007
#define INF 1000000007
#define PI 3.14159265358979323846
using namespace std;
//
int main() {
  long long K;
  long long A[50];
  cin >> K;
  cout << 50 << endl;
  FOR(i, 0, 50) { A[i] = i; }
  for (long long i = 0; i < K; i++) {
    int M = i % 50;
    FOR(j, 0, 50) {
      if (j == M) {
        A[j] += 50;
      } else {
        A[j]--;
      }
    }
  }
  FOR(i, 0, 50) {
    if (i) {
      cout << " ";
    }
    cout << A[i];
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define MA(i, j) make_pair(i, j)
#define PA pair<int, int>
#define PB push_back
#define PQ priority_queue<int>
#define PGQ priority_queue<int, vector<int>, greater<int>>
#define VE vector<int>
#define VP vector<PA>
#define YES(i) cout << (i ? "YES" : "NO") << endl
#define Yes(i) cout << (i ? "Yes" : "No") << endl
#define MOD 1000000007
#define INF 1000000007
#define PI 3.14159265358979323846
using namespace std;
//
int main() {
  long long K;
  long long A[50];
  cin >> K;
  cout << 50 << endl;
  long long M = K / 50;
  long long N = K % 50;
  FOR(i, N, 50) {
    A[i] = M;
    M++;
  }
  M++;
  FOR(i, 0, N) {
    A[i] = M;
    M++;
  }
  FOR(i, 0, 50) {
    if (i) {
      cout << " ";
    }
    cout << A[i];
  }
  cout << endl;
  return 0;
}
Problem p03648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define piii pair<pii, pii>
#define precise cout << fixed << setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
vi ans;
int32_t main() {
  BOOST;
  int k;
  cin >> k;
  int n = 50;
  int need = k % n;
  int ile = n - 1 + k / n - need;
  for (int i = 1; i <= n - 1; i++)
    ans.pb(ile);
  ile += need * (n + 1);
  assert(ile <= 1e16 + 1000);
  ans.pb(ile);
  cout << sz(ans) << "\n";
  for (auto it : ans)
    cout << it << " ";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define piii pair<pii, pii>
#define precise cout << fixed << setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
vi ans;
int32_t main() {
  BOOST;
  int k;
  cin >> k;
  int n = 50;
  for (int i = 0; i < n; i++)
    ans.pb(i + k / n);
  int ile = k % n;
  for (int i = 0; i <= ile - 1; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        ans[j] += n;
      else
        ans[j]--;
    }
  }
  cout << sz(ans) << "\n";
  for (auto it : ans)
    cout << it << " ";
  return 0;
}
Problem p03649 (C++)

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

const int N = 55;
int n;
long long a[N];
long long ans;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];

  bool found = true;

  while (found) {
    found = false;
    for (int i = 1; i <= n; ++i) {
      if (a[i] >= n) {
        found = true;

        long long add = a[i] / n;
        a[i] %= n;
        ans += add;
        for (int j = 1; j <= n; ++j) {
          if (j != i)
            a[j] += add;
        }

        break;
      }
    }

    if (!found)
      break;
  }

  cout << ans << endl;
}

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

const int N = 55;
int n;
long long a[N];
long long ans;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];

  bool found = true;

  while (found) {
    found = false;
    for (int i = 1; i <= n; ++i) {
      if (a[i] >= n) {
        found = true;

        long long add = a[i] / n;
        a[i] %= n;
        ans += add;
        for (int j = 1; j <= n; ++j) {
          if (j != i)
            a[j] += add;
        }
      }
    }

    if (!found)
      break;
  }

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

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;

const LL inf = 112345678901234567;

LL get_max(std::vector<LL> v) {
  LL res = 0;
  rep(i, v.size()) res = std::max(res, v[i]);
  return res;
}

LL get_min(std::vector<LL> v) {
  LL res = inf;
  rep(i, v.size()) res = std::min(res, v[i]);
  return res;
}

int main() {
  LL N;
  in >> N;
  std::vector<LL> a(N);
  rep(i, N) in >> a[i];

  LL ans = 0;
  while (true) {
    std::sort(a.begin(), a.end());
    if (a[N - 1] <= N - 1)
      break;
    if (a[N - 1] - a[0] <= N * N) {
      LL add = std::max<LL>(a[N - 1] - N * N, 0);
      ans += add * N;
      rep(i, N) a[i] -= add;
      do {
        rep(j, N - 1)++ a[j];
        a[N - 1] -= N;
        ++ans;
        std::sort(a.begin(), a.end());
      } while (a[N - 1] > N - 1);
      break;
    }

    LL left = 0, right = inf;
    while (right - left > 1) {
      LL mid = (right + left) / 2;
      auto a_ = a;
      rep(i, N - 1) a[i] += mid;
      a[N - 1] -= N * mid;
      if (a[N - 1] < a[N - 2] || get_max(a) <= N - 1)
        right = mid;
      else
        left = mid;
      a = a_;
    }
    rep(i, N - 1) a[i] += right;
    a[N - 1] -= N * right;
    ans += right;
  }
  out << ans << std::endl;
}


Fixed submission:
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;

const LL inf = 112345678901234567;

LL get_max(std::vector<LL> v) {
  LL res = 0;
  rep(i, v.size()) res = std::max(res, v[i]);
  return res;
}

LL get_min(std::vector<LL> v) {
  LL res = inf;
  rep(i, v.size()) res = std::min(res, v[i]);
  return res;
}

int main() {
  LL N;
  in >> N;
  std::vector<LL> a(N);
  rep(i, N) in >> a[i];

  LL ans = 0;
  while (true) {
    std::sort(a.begin(), a.end());
    if (a[N - 1] <= N - 1)
      break;
    if (a[N - 1] - a[0] <= N * N) {
      LL add = std::max<LL>(a[N - 1] - N * N, 0);
      ans += add * N;
      rep(i, N) a[i] -= add;
      do {
        rep(j, N - 1)++ a[j];
        a[N - 1] -= N;
        ++ans;
        std::sort(a.begin(), a.end());
      } while (a[N - 1] > N - 1);
      break;
    }

    LL left = 0, right = inf;
    while (right - left > 1) {
      LL mid = (right + left) / 2;
      auto a_ = a;
      rep(i, N - 1) a[i] += mid;
      a[N - 1] -= N * mid;
      if (a[N - 1] < a[0] || get_max(a) <= N - 1)
        right = mid;
      else
        left = mid;
      a = a_;
    }
    rep(i, N - 1) a[i] += right;
    a[N - 1] -= N * right;
    ans += right;
  }
  out << ans << std::endl;
}
Problem p03649 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n;
long long a[55], ret;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  while (true) {
    int ptr = max_element(a, a + n) - a;
    for (int j = 0; j < n; j++) {
      if (j != ptr)
        a[j] += a[ptr] / n;
    }
    ret += a[ptr] / n;
    a[ptr] %= n;
  }
  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n;
long long a[55], ret;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  while (true) {
    int ptr = max_element(a, a + n) - a;
    if (a[ptr] < n)
      break;
    for (int j = 0; j < n; j++) {
      if (j != ptr)
        a[j] += a[ptr] / n;
    }
    ret += a[ptr] / n;
    a[ptr] %= n;
  }
  cout << ret << endl;
  return 0;
}
Problem p03649 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

void solve(long long N, vector<long long> a) {
  ll amax = 0;
  rep(i, N) { amax = max(amax, a[i]); }
  ll res = 0;
  while (amax > N - 1) {
    ll se = -1;
    rep(i, N) {
      if (a[i] > N - 1) {
        se = i;
        break;
      }
    }
    ll minus = a[se] / N;
    // cout << minus << endl;
    res += minus;
    rep(i, N) {
      if (i == se)
        a[i] = a[i] % N;
      else
        a[i] += minus;
    }
    amax = 0;
    rep(i, N) { amax = max(amax, a[i]); }
  }
  cout << res << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  vector<long long> a(N - 1 + 1);
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &a[i]);
  }
  solve(N, a);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

void solve(long long N, vector<long long> a) {
  ll amax = 0;
  rep(i, N) { amax = max(amax, a[i]); }
  ll res = 0;
  while (amax > N - 1) {
    ll s = 0;
    rep(i, N) { s += a[i] / N; }
    res += s;
    rep(i, N) { a[i] = a[i] % N + (s - a[i] / N); }
    amax = 0;
    rep(i, N) { amax = max(amax, a[i]); }
  }
  cout << res << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  vector<long long> a(N - 1 + 1);
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &a[i]);
  }
  solve(N, a);
  return 0;
}
Problem p03649 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 60;
ll a[N];
ll n;
ll cnt;

int main() {
  freopen("in.txt", "r", stdin);
  cnt = 0;
  scanf("%lld", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  sort(a, a + n);
  while (a[n - 1] >= n) {
    ll tmp = a[n - 1] / n;
    cnt += tmp;
    a[n - 1] -= tmp * n;
    for (int i = 0; i < n - 1; i++)
      a[i] += tmp;
    sort(a, a + n);
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 60;
ll a[N];
ll n;
ll cnt;

int main() {
  // freopen("in.txt","r",stdin);
  cnt = 0;
  scanf("%lld", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  sort(a, a + n);
  while (a[n - 1] >= n) {
    ll tmp = a[n - 1] / n;
    cnt += tmp;
    a[n - 1] -= tmp * n;
    for (int i = 0; i < n - 1; i++)
      a[i] += tmp;
    sort(a, a + n);
  }
  cout << cnt << endl;
  return 0;
}
Problem p03649 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {

  long long int n;
  cin >> n;

  long long int ans = 0;
  vector<long long int> v;

  for (long long int i = 0; i < n; i++) {
    long long int in;
    cin >> in;
    v.push_back(in);
  }

  while (true) {

    bool f = true;
    for (long long int i = 0; i < n; i++) {
      if (v[i] >= n) {
        f = false;
        long long int cnt = v[i] / n;
        ans += cnt;
        v[i] %= n;
        for (long long int j = 0; j < n; j++) {
          if (j == i)
            continue;
          v[j] += cnt;
        }
        break;
      }
    }
    if (f == true)
      break;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {

  long long int n;
  cin >> n;

  long long int ans = 0;
  vector<long long int> v;

  for (long long int i = 0; i < n; i++) {
    long long int in;
    cin >> in;
    v.push_back(in);
  }

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

  if (v[n - 1] > n * (n - 2) + n - 2) {

    ans += n - 2;
    v[n - 1] -= n * (n - 2);
    for (long long int j = 0; j < n - 1; j++) {
      v[j] += n - 2;
    }

    for (long long int i = 0; i < n; i++) {
      if (v[i] > (n + 1) * 2) {
        long long int cnt = (v[i] - (n + 1) * 2) / (n + 1) + 1;
        ans += cnt * (n + 1);
        v[i] -= cnt * (n + 1);
      }
    }
  }

  while (true) {

    bool f = true;
    for (long long int i = 0; i < n; i++) {
      if (v[i] >= n) {
        f = false;
        long long int cnt = v[i] / n;
        ans += cnt;
        v[i] %= n;
        for (long long int j = 0; j < n; j++) {
          if (j == i)
            continue;
          v[j] += cnt;
        }
        break;
      }
    }
    if (f == true)
      break;
  }

  cout << ans << endl;

  return 0;
}
Problem p03649 (C++)

Buggy submission (status: Time Limit Exceeded):
// 21:57 - 22:02
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;

const int N = 50 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

ll s[N];

int main() {
  ios::sync_with_stdio(0);

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> s[i];

  ll ans = 0;
  bool f = 1;
  while (f) {
    f = 0;
    for (int i = 1; i <= n; i++) {
      if (s[i] >= n) {
        f = 1;
        ll d = (s[i] - (n - 1) + n - 1) / n;
        s[i] -= d * n;
        for (int j = 1; j <= n; j++)
          if (i != j)
            s[j] += d;
        ans += d;
        break;
      }
    }
    if (!f)
      break;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
// 21:57 - 22:02
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;

const int N = 50 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

ll s[N];

int main() {
  ios::sync_with_stdio(0);

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> s[i];

  ll ans = 0;
  bool f = 1;
  while (f) {
    f = 0;
    for (int i = 1; i <= n; i++) {
      if (s[i] >= n) {
        f = 1;
        ll d = (s[i] - (n - 1) + n - 1) / n;
        s[i] -= d * n;
        for (int j = 1; j <= n; j++)
          if (i != j)
            s[j] += d;
        ans += d;
      }
    }
    if (!f)
      break;
  }

  cout << ans << endl;

  return 0;
}
Problem p03649 (C++)

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

using namespace std;

const long long MOD = 1000000007LL;
long long GCD(long long a, long long b) { return b == 0 ? a : GCD(b, a % b); }
long long fast_exp(long long base, long long exp, long long mod = MOD) {
  long long tot = 1;
  for (; exp > 0; exp >>= 1) {
    if ((exp & 1) == 1)
      tot = tot * base % mod;
    base = base * base % mod;
  }
  return tot;
}

long long arr[51], cur[51];

int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;

  long long diff, tot = 0;

  for (int i = 0; i < n; ++i)
    cin >> arr[i];

  for (int i = 0; i < 3125000; ++i) {
    for (int j = 0; j < n; ++j) {
      diff = (arr[j] + tot - cur[j]) - (n - 1);
      diff = diff / n + (diff % n > 0);
      tot += diff;
      cur[j] += diff;
      arr[j] -= n * diff;
      // cout << arr[j] + tot - cur[j] << " ";
    }
    // cout << endl;
  }

  cout << tot << "\n";

  return 0;
}


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

using namespace std;

const long long MOD = 1000000007LL;
long long GCD(long long a, long long b) { return b == 0 ? a : GCD(b, a % b); }
long long fast_exp(long long base, long long exp, long long mod = MOD) {
  long long tot = 1;
  for (; exp > 0; exp >>= 1) {
    if ((exp & 1) == 1)
      tot = tot * base % mod;
    base = base * base % mod;
  }
  return tot;
}

long long arr[51], cur[51];

int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;

  long long diff, tot = 0;

  for (int i = 0; i < n; ++i)
    cin >> arr[i];

  for (int i = 0; i < 625000; ++i) {
    for (int j = 0; j < n; ++j) {
      diff = (arr[j] + tot - cur[j]) - (n - 1);
      diff = diff / n + (diff % n > 0);
      tot += diff;
      cur[j] += diff;
      arr[j] -= n * diff;
      // cout << arr[j] + tot - cur[j] << " ";
    }
    // cout << endl;
  }

  cout << tot << "\n";

  return 0;
}
Problem p03649 (C++)

Buggy submission (status: Time Limit Exceeded):
/*input
10
1000 193 256 777 0 1 1192 1234567891011 48 425
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define REP(i, j, k) for (int i = j; i < k; ++i)
#define RREP(i, j, k) for (int i = j; i >= k; --i)
#define A first
#define B second
#define mp make_pair
#define pb emplace_back
#define PII pair<int, int>
#define MEM(i, j) memset(i, j, sizeof i)
#define ALL(i) i.begin(), i.end()
#define DBGG(i, j) cout << i << " " << j << endl
#define DB4(i, j, k, l) cout << i << " " << j << " " << k << " " << l << endl
#define IOS cin.tie(0), cout.sync_with_stdio(0)
// #define endl "\n"
///------------------------------------------------------------
#define int long long
#define MAX 500
#define INF 0x3f3f3f3f

int n, x[MAX];
int solve() {
  int big = 0;
  REP(i, 1, n + 1) big = max(big, x[i]);
  if (big < n)
    return 0;
  REP(i, 1, n + 1) if (x[i] == big) {
    REP(j, 1, n + 1) x[j]++;
    x[i] += -1 - n;
    return 1;
  }
}
int32_t main() {
  IOS;
  cin >> n;
  int ans = 0;
  REP(i, 1, n + 1) cin >> x[i];
  REP(times, 0, 500) {

    REP(i, 1, n + 1) if (x[i] >= n) {
      int tmp = x[i] / n;
      x[i] %= n;
      ans += tmp;
      // if(tmp >= n) ans += (tmp - (n - 1)) * n , tmp = n - 1;
      REP(j, 1, n + 1) if (i != j) x[j] += tmp;
    }
    // REP(i , 1 , n + 1) cout << x[i] << " " ; cout << endl;
    // DBGG("now ans = " , ans);
  }
  while (solve())
    ans++;
  cout << ans << endl;
  return 0;
}

Fixed submission:
/*input
10
1000 193 256 777 0 1 1192 1234567891011 48 425
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define REP(i, j, k) for (int i = j; i < k; ++i)
#define RREP(i, j, k) for (int i = j; i >= k; --i)
#define A first
#define B second
#define mp make_pair
#define pb emplace_back
#define PII pair<int, int>
#define MEM(i, j) memset(i, j, sizeof i)
#define ALL(i) i.begin(), i.end()
#define DBGG(i, j) cout << i << " " << j << endl
#define DB4(i, j, k, l) cout << i << " " << j << " " << k << " " << l << endl
#define IOS cin.tie(0), cout.sync_with_stdio(0)
// #define endl "\n"
///------------------------------------------------------------
#define int long long
#define MAX 500
#define INF 0x3f3f3f3f

int n, x[MAX];
int solve() {
  int big = 0;
  REP(i, 1, n + 1) big = max(big, x[i]);
  if (big < n)
    return 0;
  REP(i, 1, n + 1) if (x[i] == big) {
    REP(j, 1, n + 1) x[j]++;
    x[i] += -1 - n;
    return 1;
  }
}
int32_t main() {
  IOS;
  cin >> n;
  int ans = 0;
  REP(i, 1, n + 1) cin >> x[i];
  REP(times, 0, 3000000 / n / n) {

    REP(i, 1, n + 1) if (x[i] >= n) {
      int tmp = x[i] / n;
      x[i] %= n;
      ans += tmp;
      // if(tmp >= n) ans += (tmp - (n - 1)) * n , tmp = n - 1;
      REP(j, 1, n + 1) if (i != j) x[j] += tmp;
    }
    // REP(i , 1 , n + 1) cout << x[i] << " " ; cout << endl;
    // DBGG("now ans = " , ans);
  }
  while (solve())
    ans++;
  cout << ans << endl;
  return 0;
}
Problem p03649 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define debug(x) cerr << #x << '=' << x << '\n'

namespace T {
int l[23333], r[23333];
int size[23333];
long long v[23333];
int root = 0, tot = 0;
int merge(int x, int y) {
  if (!x || !y) {
    return x + y;
  }
  if (v[x] < v[y]) {
    swap(x, y);
  }
  r[x] = merge(r[x], y);
  size[x] += size[y];
  if (size[l[x]] < size[r[x]]) {
    swap(l[x], r[x]);
  }
  return x;
}
inline long long top(void) {
  if (!root) {
    return -0x3f3f3f3f;
  } else {
    return v[root];
  }
}
inline void pop(void) { root = merge(l[root], r[root]); }
inline void insert(long long x) {
  ++tot;
  l[tot] = r[tot] = 0;
  size[tot] = 1;
  v[tot] = x;
  root = merge(root, tot);
}
} // namespace T

long long a[66];

int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", a + i);
    T::insert(a[i]);
  }
  long long k = 0;
  while (T::top() + k >= n) {
    long long p = T::top() + k;
    //	debug(p);
    T::pop();
    k += p / n;
    //	debug(k);
    p %= n;
    //	debug(p);
    T::insert(p - k);
  }
  cout << k;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define debug(x) cerr << #x << '=' << x << '\n'

namespace T {
int l[23333333], r[23333333];
int size[23333333];
long long v[23333333];
int root = 0, tot = 0;
int merge(int x, int y) {
  if (!x || !y) {
    return x + y;
  }
  if (v[x] < v[y]) {
    swap(x, y);
  }
  r[x] = merge(r[x], y);
  size[x] += size[y];
  if (size[l[x]] < size[r[x]]) {
    swap(l[x], r[x]);
  }
  return x;
}
inline long long top(void) {
  if (!root) {
    return -0x3f3f3f3f;
  } else {
    return v[root];
  }
}
inline void pop(void) { root = merge(l[root], r[root]); }
inline void insert(long long x) {
  ++tot;
  l[tot] = r[tot] = 0;
  size[tot] = 1;
  v[tot] = x;
  root = merge(root, tot);
}
} // namespace T

long long a[66];

int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", a + i);
    T::insert(a[i]);
  }
  long long k = 0;
  while (T::top() + k >= n) {
    long long p = T::top() + k;
    //	debug(p);
    T::pop();
    k += p / n;
    //	debug(k);
    p %= n;
    //	debug(p);
    T::insert(p - k);
  }
  cout << k;
  return 0;
}
Problem p03649 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
const LL INF = 1e18;
const int MAXN = 60;
const double eps = 1e-10;
LL a[MAXN];

int main() {
  LL n;
  while (scanf("%lld", &n) != EOF) {
    for (int i = 1; i <= n; ++i)
      scanf("%lld", &a[i]);
    bool flag = true;
    LL ans = 0, div = 0;
    int p = 0;
    while (flag) {
      flag = false;
      for (int i = 1; i <= n; i++) {
        if (a[i] > n - 1) {
          flag = true;
          p = i;
        }
      }
      if (flag) {
        div = a[p] / n;
        ans += div;
        a[p] %= n;
        for (int i = 1; i <= n; i++)
          if (i != p)
            a[i] += div;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
const LL INF = 1e18;
const int MAXN = 60;
const double eps = 1e-10;
LL a[MAXN];

int main() {
  LL n;
  while (scanf("%lld", &n) != EOF) {
    for (int i = 1; i <= n; ++i)
      scanf("%lld", &a[i]);
    bool flag = true;
    LL ans = 0, div = 0;
    int p = 0;
    while (flag) {
      p = 1;
      if (a[p] < n)
        flag = false;
      for (int i = 2; i <= n; i++) {
        if (a[i] > a[p] && a[i] >= n) {
          flag = true;
          p = i;
        }
      }
      if (flag) {
        div = a[p] / n;
        ans += div;
        a[p] %= n;
        for (int i = 1; i <= n; i++)
          if (i != p)
            a[i] += div;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p03649 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
namespace not_mine {
typedef long long ll;
const int N = 55;
const ll INF = 123372036854775801ll;
ll s[N], ans;
int n;
void work() {
  int p = 0;
  for (int i = 1; i <= n; s[i++]++)
    if (s[i] > s[p])
      p = i;
  s[p] -= n + 1;
}
bool check() {
  for (int i = 1; i <= n; i++)
    if (s[i] >= n)
      return 0;
  return 1;
}
ll calc(ll x) {
  ll ret = 0;
  for (int i = 1; i <= n; i++)
    if (s[i] > x)
      ret += (s[i] - x) / (n + 1);
  return ret;
}
void initialize() {
  ll tmp = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", s + i), tmp += s[i];
  ll u = INF, d = 0, cnt = tmp - (ll)(n - 1) * (n - 1);
  for (int i = 1; i <= n; i++)
    s[i] += cnt;

  for (ll mid; u > d;) {
    mid = (u + d) >> 1;
    if (calc(mid) <= cnt)
      u = mid;
    else
      d = mid + 1;
  }
  ans = calc(d), cnt -= ans;
  for (int i = 1; i <= n; i++)
    s[i] -= (n + 1) * ((s[i] - d) / (n + 1)) + cnt;
}
void solve() {
  initialize();
  for (; !check(); ans++)
    work();
  printf("%lld\n", ans);
}
} // namespace not_mine
int main() {
  not_mine::solve();
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
namespace not_mine {
typedef long long ll;
const int N = 55;
const ll INF = 123372036854775801ll;
ll s[N], ans;
int n;
void work() {
  int p = 0;
  for (int i = 1; i <= n; s[i++]++)
    if (s[i] > s[p])
      p = i;
  s[p] -= n + 1;
}
bool check() {
  for (int i = 1; i <= n; i++)
    if (s[i] >= n)
      return 0;
  return 1;
}
ll calc(ll x) {
  ll ret = 0;
  for (int i = 1; i <= n; i++)
    if (s[i] > x)
      ret += (s[i] - x) / (n + 1);
  return ret;
}
void initialize() {
  ll tmp = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", s + i), tmp += s[i];
  ll u = INF, d = 0, cnt = tmp - (ll)(n - 1) * (n - 1);
  for (int i = 1; i <= n; i++)
    s[i] += cnt;

  for (ll mid; u > d;) {
    mid = (u + d) >> 1;
    if (calc(mid) <= cnt)
      u = mid;
    else
      d = mid + 1;
  }
  ans = calc(d), cnt -= ans;
  for (int i = 1; i <= n; s[i++] -= cnt)
    if (s[i] > d)
      s[i] -= (n + 1) * ((s[i] - d) / (n + 1));
}
void solve() {
  initialize();
  for (; !check(); ans++)
    work();
  printf("%lld\n", ans);
}
} // namespace not_mine
int main() {
  not_mine::solve();
  return 0;
}
Problem p03649 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

using ll = long long;
int main() {
  int n;
  cin >> n;
  vector<ll> A(n);
  ll high = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    high += A[i];
  }
  ll low = -1;
  while (high - low > 1) {
    ll H = high;
    ll mean = (low + high) / 2;
    for (ll M = min(low, mean - n); M <= mean; M++) {
      vector<ll> B(A);
      for (int i = 0; i < n; i++) {
        B[i] += M;
      }
      ll k = 0;
      for (int i = 0; i < n; i++) {
        if (B[i] > n - 1) {
          k += (B[i] - n + 1) / (n + 1);
          if ((B[i] - n + 1) % (n + 1) != 0) {
            k++;
          }
        }
      }
      if (k <= M) {
        high = M;
        break;
      }
    }
    if (high == H) {
      low = mean;
    }
  }
  cout << high << endl;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

using ll = long long;
int main() {
  int n;
  cin >> n;
  vector<ll> A(n);
  ll high = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    high += A[i];
  }
  ll low = -1;
  while (high - low > 1) {
    ll H = high;
    ll mean = (low + high) / 2;
    for (ll M = max(low, mean - n); M <= mean; M++) {
      vector<ll> B(A);
      for (int i = 0; i < n; i++) {
        B[i] += M;
      }
      ll k = 0;
      for (int i = 0; i < n; i++) {
        if (B[i] > n - 1) {
          k += (B[i] - n + 1) / (n + 1);
          if ((B[i] - n + 1) % (n + 1) != 0) {
            k++;
          }
        }
      }
      if (k <= M) {
        high = M;
        break;
      }
    }
    if (high == H) {
      low = mean;
    }
  }
  cout << high << endl;
}
Problem p03649 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#define LL long long
using namespace std;
LL i, j, k, n, f, trs, bgs, ans;
LL a[55];
int main() {
  scanf("%lld", &n);
  for (i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  f = 0;
  while (!f) {
    f = 1;
    for (i = 1; i <= n; ++i)
      if (a[i] > (n << 1)) {
        f = 0;
        trs = (a[i] - (n << 1) - 1) / n + 1;
        ans += trs;
        a[i] -= trs * n;
        for (j = 1; j <= n; ++j)
          if (i != j)
            a[j] += trs;
        break;
      }
  }
  while (true) {
    bgs = 1;
    for (i = 2; i <= n; ++i)
      if (a[bgs] < a[i])
        bgs = i;
    if (a[bgs] < n)
      break;
    else {
      ++ans;
      a[bgs] -= n;
      for (i = 1; i <= n; ++i)
        if (i != bgs)
          ++a[i];
    }
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#define LL long long
using namespace std;
LL i, j, k, n, f, trs, bgs, ans;
LL a[55];
int main() {
  scanf("%lld", &n);
  for (i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  f = 0;
  while (!f) {
    f = 1;
    for (i = 1; i <= n; ++i)
      if (a[i] > (n << 1)) {
        f = 0;
        trs = (a[i] - (n << 1) - 1) / n + 1;
        ans += trs;
        a[i] -= trs * n;
        for (j = 1; j <= n; ++j)
          if (i != j)
            a[j] += trs;
      }
  }
  while (true) {
    bgs = 1;
    for (i = 2; i <= n; ++i)
      if (a[bgs] < a[i])
        bgs = i;
    if (a[bgs] < n)
      break;
    else {
      ++ans;
      a[bgs] -= n;
      for (i = 1; i <= n; ++i)
        if (i != bgs)
          ++a[i];
    }
  }
  printf("%lld", ans);
  return 0;
}
Problem p03649 (C++)

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

using namespace std;

const int N = 50;

int main() {
  int n;
  long long int a[N + 1];
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &a[i]);
  }
  long long res = 0;
  bool find = true;
  while (find) {
    find = false;
    for (int i = 1; !find && i <= n; ++i) {
      if (a[i] >= n) {
        find = true;
        long long g = a[i] / n;
        res += g;
        a[i] %= n;
        for (int j = 1; j <= n; ++j) {
          if (j != i) {
            a[j] += g;
          }
        }
      }
    }
  }
  printf("%lld\n", res);
  return 0;
}


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

using namespace std;

const int N = 50;

int main() {
  int n;
  long long int a[N + 1];
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &a[i]);
  }
  long long res = 0;
  bool find = true;
  while (find) {
    find = false;
    for (int i = 1; i <= n; ++i) {
      if (a[i] >= n) {
        find = true;
        long long g = a[i] / n;
        res += g;
        a[i] %= n;
        for (int j = 1; j <= n; ++j) {
          if (j != i) {
            a[j] += g;
          }
        }
      }
    }
  }
  printf("%lld\n", res);
  return 0;
}
Problem p03649 (C++)

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

using namespace std;

int main() {
  int n;
  cin >> n;
  long long a[30];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long ans = 0;
  while (true) {
    long long tmp = 0;
    bool ok = true;
    for (int i = 0; i < n; i++) {
      tmp += a[i] / n;
      if (a[i] >= n)
        ok = false;
    }
    if (ok)
      break;
    ans += tmp;
    for (int i = 0; i < n; i++)
      a[i] = a[i] % n + tmp - a[i] / n;
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

int main() {
  int n;
  cin >> n;
  long long a[50];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long ans = 0;
  while (true) {
    long long tmp = 0;
    bool ok = true;
    for (int i = 0; i < n; i++) {
      tmp += a[i] / n;
      if (a[i] >= n)
        ok = false;
    }
    if (ok)
      break;
    ans += tmp;
    for (int i = 0; i < n; i++)
      a[i] = a[i] % n + tmp - a[i] / n;
  }
  cout << ans << endl;

  return 0;
}
Problem p03649 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
ans = 0
flag = True
while flag:
    flag = False
    for i in range(N):
        if A[i] >= N:
            count = A[i] // N
            ans += count
            for j in range(N):
                A[j] += count
            A[i] -= count
            A[i] %= N
            flag = True
            break

print(ans)


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
ans = 0
flag = True
while flag:
    flag = False
    for i in range(N):
        if A[i] >= N:
            count = A[i] // N
            ans += count
            for j in range(N):
                A[j] += count
            A[i] -= count
            A[i] %= N
            flag = True

print(ans)