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

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

using namespace std;
#define ll long long
#define ld long double
#define rep(i, h) for (int i = 0; i < h; ++i)
#define rep1(i, h) for (int i = 0; i <= h; ++i)
#define rep2(i, k, h) for (int i = k; i < h; ++i)
#define rep3(i, k, h) for (int i = k; i <= h; ++i)
#define INF LONG_LONG_MAX // long long INF
#define inf INT_MAX       // int inf

typedef pair<int, int> int_pair;
typedef pair<string, string> string_pair;

int main() {
  int n;
  cin >> n;
  ll ans = 0;
  rep2(i, 1, n) { ans += (i % (i + 1)); }
  cout << ans << endl;
  return 0;
}


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

using namespace std;
#define ll long long
#define ld long double
#define rep(i, h) for (int i = 0; i < h; ++i)
#define rep1(i, h) for (int i = 0; i <= h; ++i)
#define rep2(i, k, h) for (int i = k; i < h; ++i)
#define rep3(i, k, h) for (int i = k; i <= h; ++i)
#define INF LONG_LONG_MAX // long long INF
#define inf INT_MAX       // int inf

typedef pair<int, int> int_pair;
typedef pair<string, string> string_pair;

int main() {
  int n;
  cin >> n;
  ll ans = 0;
  rep2(i, 1, n) { ans += i; }
  cout << ans << endl;
  return 0;
}
Problem p02924 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, c, m = 0;
  cin >> n;
  int ar[n + 1], j = 1;
  ar[1] = n;
  for (int i = 2; i <= n; i++) {
    ar[i] = i - 1;
  }
  for (int k = 1; k <= n; k++)
    m = m + ar[k] % k - 1;
  cout << m;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, c, m = 0;
  cin >> n;
  /*  int ar[n],j=1;
    ar[1]=n;
    for(int i=2;i<=n;i++){
      ar[i]=j;
      j++;
    }
    for(int k=1;k<=n;k++)
      m=m+ar[k]%k;*/
  m = n * (n - 1) / 2;
  cout << m;
}
Problem p02924 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long int n, sum = 0;
  cin >> n;
  while (n != 1) {
    sum = sum + (n - 1) % (n);
    n--;
  }
  cout << sum;
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long int n, sum = 0;
  cin >> n;
  for (int i = 1; i < n; i++) {
    sum = sum + i;
  }
  cout << sum;
  return 0;
}
Problem p02924 (C++)

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

int main() {
  int N;
  cin >> N;
  long long int ans = 0;
  for (int i = 1; i <= N - 1; i++) {
    ans += i % (i + 1);
  }
  cout << ans << endl;
}


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

int main() {
  int N;
  cin >> N;
  long long int ans = 0;
  for (int i = 1; i <= N - 1; i++) {
    ans += i;
  }
  cout << ans << endl;
}
Problem p02924 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

// output
#define SPBR(w, n) std::cout << (w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline T chmax(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T chmin(T &a, const T b) {
  return a = (a > b) ? b : a;
}

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr int MOD = 1000000007;
constexpr int INF = 1e18;

using namespace std;

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

  vector<vector<int>> A(N, vector<int>(N - 1));
  REP(i, N) REP(j, N - 1) {
    cin >> A[i][j];
    A[i][j]--;
  }

  vector<int> cnt(N);
  vector<int> flag(N);
  vector<int> ok(N);
  int ans = 0;

  while (true) {
    fill(ALL(flag), 0);
    int change = 0;
    REP(i, N) {
      int j = A[i][cnt[i]];
      if (ok[i] || flag[i])
        continue;
      if (ok[j] || flag[j])
        continue;

      if (i == A[j][cnt[j]]) {
        change = 1;
        cnt[i]++;
        cnt[j]++;
        flag[i] = 1;
        flag[j] = 1;
        if (cnt[i] == N - 1)
          ok[i] = 1;
        if (cnt[j] == N - 1)
          ok[j] = 1;
      }
    }
    if (change)
      ans++;
    else
      break;
  }

  REP(i, N) if (ok[i] == 0) ans = -1;
  cout << ans << endl;

  return 0;
}

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

// output
#define SPBR(w, n) std::cout << (w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline T chmax(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T chmin(T &a, const T b) {
  return a = (a > b) ? b : a;
}

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr int MOD = 1000000007;
constexpr int INF = 1e18;

using namespace std;

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

  cout << N * (N - 1) / 2 << endl;

  return 0;
}
Problem p02924 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
typedef long long int ll;
using namespace std;

int main() {
  ll N;
  cin >> N;
  vector<ll> P(N);
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    P.at(i) = i;
    ans += P.at(i);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
typedef long long int ll;
using namespace std;

int main() {
  ll N;
  cin >> N;
  ll ans = (N * (N - 1)) / 2;
  cout << ans << endl;
}
Problem p02924 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>

#define PI 3.14159265359
#define rep(i, a, n) for (int i = a; i < (int)n; ++i)
#define SZ(x) ((int)(x).size()) // size() unsigned -> int
#define descSort(a) sort(a.begin(), a.end(), std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
ll LCM(int a, int b) { return a * b / gcd(a, b); }

int main(void) {
  ll n;
  cin >> n;
  ll ans = 0;
  rep(i, 0, n - 1) { ans += (i + 1) % (i + 2); }
  cout << ans << endl;
  return 0;
}

Fixed submission:

#include <bits/stdc++.h>

#define PI 3.14159265359
#define rep(i, a, n) for (int i = a; i < (int)n; ++i)
#define SZ(x) ((int)(x).size()) // size() unsigned -> int
#define descSort(a) sort(a.begin(), a.end(), std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;
ll gcd(ll x, ll y) {
  if (x % y == 0)
    return y;
  return gcd(y, x % y);
}
ll LCM(int a, int b) { return a * b / gcd(a, b); }

int main(void) {
  ll n;
  cin >> n;
  ll ans = n * (n - 1) / 2;
  cout << ans << endl;
  return 0;
}
Problem p02924 (C++)

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

typedef long long ll;
typedef pair<ll, ll> pll;

#define m0(x) memset(x, 0, sizeof(x))
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define asort(x) sort(all(x));
#define dsort(x, t) sort(x.begin(), x.end(), greater<t>());
#define dump(x) cout << #x << " = " << (x) << endl
#define vuniq(v) v.erase(unique(v.begin(), v.end()), v.end());
#define minse(m, s) m.insert(make_pair(s, 1));
#define mfin(m, s) m.find(s) != m.end()

#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl

const int INF = 1e9;
const ll LINF = 1e18;
const int mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;

  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ll p = i + 1;
    if (i == n) {
      p = 1;
    }
    ans += i % p;
  }

  cout << ans << endl;
  return 0;
}

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

typedef long long ll;
typedef pair<ll, ll> pll;

#define m0(x) memset(x, 0, sizeof(x))
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define asort(x) sort(all(x));
#define dsort(x, t) sort(x.begin(), x.end(), greater<t>());
#define dump(x) cout << #x << " = " << (x) << endl
#define vuniq(v) v.erase(unique(v.begin(), v.end()), v.end());
#define minse(m, s) m.insert(make_pair(s, 1));
#define mfin(m, s) m.find(s) != m.end()

#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl

const int INF = 1e9;
const ll LINF = 1e18;
const int mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;

  ll ans = (n - 1) * n / 2;
  cout << ans << endl;
  return 0;
}
Problem p02924 (C++)

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

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll inf = 1LL << 60;
const double eps = 1e-9;

int main() {
  ll n;
  cin >> n;
  cout << n / (n - 1) / 2 << endl;
  return 0;
}


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

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll inf = 1LL << 60;
const double eps = 1e-9;

int main() {
  ll n;
  cin >> n;
  cout << n * (n - 1) / 2 << endl;
  return 0;
}
Problem p02924 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 998244353
#define INF 10000000000000001
#define F first
#define S second
#define LB lower_bound
#define UB upper_bound
#define vc vector
#define vll vector<long long>
#define pll pair<long long, long long>
#define pb push_back
#define ll long long
#define all(v) v.begin(), v.end()
#define T                                                                      \
  ll test;                                                                     \
  cin >> test;                                                                 \
  while (test--)
#define rep(i, a, n) for (ll i = a; i < (long long)n; ++i)
#define repr(i, n, a) for (ll i = n; i > (long long)a; --i)
#define MAX 1000005
using namespace std;
int main() {
  ll n, cnt = 0, ans = 0;
  cin >> n;
  ll ar[n + 1];
  for (ll i = n; i >= 0; i--) {
    ar[i] = i - 1;
  }
  rep(i, 1, n + 1) { ans += ar[i] % i; }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define MOD 998244353
#define INF 10000000000000001
#define F first
#define S second
#define LB lower_bound
#define UB upper_bound
#define vc vector
#define vll vector<long long>
#define pll pair<long long, long long>
#define pb push_back
#define ll long long
#define all(v) v.begin(), v.end()
#define T                                                                      \
  ll test;                                                                     \
  cin >> test;                                                                 \
  while (test--)
#define rep(i, a, n) for (ll i = a; i < (long long)n; ++i)
#define repr(i, n, a) for (ll i = n; i > (long long)a; --i)
#define MAX 1000005
using namespace std;
int main() {
  ll n, cnt = 0, ans = 0;
  cin >> n;
  ll val = n - 1;
  ans = (val + 1) * (val);
  cout << ans / 2 << endl;
}
Problem p02924 (C++)

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

using namespace std;

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

int main() {
  ll n, m, i, j, k;
  cin >> n;
  vector<ll> v(n);
  for (i = 1; i < n; i++) {
    v[i - 1] = i + 1;
  }
  v[n - 1] = 1;
  ll sum = 0;
  for (i = 1; i <= n; i++) {
    sum += i % v[i - 1];
  }
  cout << sum << endl;
  return 0;
}

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

using namespace std;

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

int main() {
  ll n, m, i, j, k;
  cin >> n;
  cout << n * (n - 1) / 2 << endl;
  return 0;
}
Problem p02924 (C++)

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

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

Int INF = 1e18;
int inf = 1e9;
Int mod = 1e9 + 7;

int main() {
  Int n;
  cin >> n;
  Int res = 0;
  for (Int i = 1; i <= n; i++) {
    res += i % ((i % n) + 1);
  }
  dump(res);
  return 0;
}

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

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

Int INF = 1e18;
int inf = 1e9;
Int mod = 1e9 + 7;

int main() {
  Int n;
  cin >> n;
  dump(n * (n - 1) / 2);
  return 0;
}
Problem p02924 (C++)

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

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

#define ALL(c) c.begin(), c.end()
#define SORT(c) std::sort(ALL(c))
#define RSORT(c) std::sort(ALL(c), std::greater<decltype(c)::value_type>())

using namespace std;
using ll = long long;

const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 1;
const ll LINF = (ll)1e18 + 1;

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

  ll N;
  std::cin >> N;

  vector<ll> A(N);
  iota(ALL(A), 0);
  A[0] = N;

  ll ans = 0;
  REP(i, N) { ans += A[i] % (i + 1); }

  std::cout << ans << std::endl;

  return 0;
}


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

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

#define ALL(c) c.begin(), c.end()
#define SORT(c) std::sort(ALL(c))
#define RSORT(c) std::sort(ALL(c), std::greater<decltype(c)::value_type>())

using namespace std;
using ll = long long;

const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 1;
const ll LINF = (ll)1e18 + 1;

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

  ll N;
  std::cin >> N;
  std::cout << (N * (N - 1)) / 2 << std::endl;

  return 0;
}
Problem p02924 (C++)

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

using namespace std;

typedef long long ll;

const double pi = 3.14159265358979323846;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define mod 1000000007LL
#define PS(p) cout << setprecision(15) << fixed << p << endl;
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
#define ALL(c) c.begin(), c.end()
#define CIN(v) cin >> v;
#define COUT(v) cout << v << endl;

int main() {
  ll N;

  CIN(N);
  ll c = 0;
  ll m = 0;
  ll t = 0;
  for (ll i = 1; i < N; i++) {
    t = (i) % (N + i);
    m += t;
    // COUT(m << " t " << t << " i " << i + 1 << " n " << N - i);
  }

  COUT(m);

  return 0;
}


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

using namespace std;

typedef long long ll;

const double pi = 3.14159265358979323846;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define mod 1000000007LL
#define PS(p) cout << setprecision(15) << fixed << p << endl;
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
#define ALL(c) c.begin(), c.end()
#define CIN(v) cin >> v;
#define COUT(v) cout << v << endl;

int main() {
  ll N;

  CIN(N);
  COUT(N * (N - 1) / 2);

  return 0;
}
Problem p02924 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, sum = 0, tmp;
  cin >> n;
  for (int i = 1; i < n; i++) {
    tmp = i % (i + 1);
    sum += tmp;
  }
  cout << sum << '\n';
}


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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, sum = 0, tmp;
  cin >> n;
  for (int i = 1; i < n; i++) {
    sum += i;
  }
  cout << sum << '\n';
}
Problem p02924 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const double PI = acos(-1);

int main() {
  long n;
  cin >> n;
  long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i != n)
      ans += i % (i + 1);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const double PI = acos(-1);

int main() {
  long n;
  cin >> n;
  long ans = n * (n - 1) / 2;
  cout << ans << endl;
  return 0;
}
Problem p02924 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  ll i, j, k, ans = 0, cnt = 0, N, tmp, at, M = 0;

  scanf("%lld", &N);
  for (i = 1; i <= N - 1; i++) {
    j = i + 1;
    M += i % j;
  }
  printf("%lld", M);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  ll i, j, k, ans = 0, cnt = 0, N, tmp, at, M = 0;

  scanf("%lld", &N);
  for (i = 1; i <= N - 1; i++) {
    M += i;
  }
  printf("%lld", M);

  return 0;
}
Problem p02924 (C++)

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

int main() {
  long long n;
  cin >> n;

  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i == n)
      ans += i % 1;
    else
      ans += i % (i + 1);
  }

  cout << ans << endl;
  return 0;
}

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

int main() {
  long long n;
  cin >> n;

  long long ans = n * (n - 1) / 2;

  cout << ans << endl;
  return 0;
}
Problem p02924 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;

int main() {
  ll n;
  cin >> n;
  ll a, b;
  ll count = 0;
  for (int i = 1; i < n; i++) {
    a = i % (i + 1);
    count = count + a;
  }
  cout << count << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;

int main() {
  ll n;
  cin >> n;

  ll a = n * (n - 1) / 2;
  cout << a << endl;
}
Problem p02924 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
inline constexpr ll gcd(ll a, ll b) {
  if (!a || !b)
    return 0;
  while (b) {
    ll c = b;
    b = a % b;
    a = c;
  }
  return a;
}
inline constexpr ll lcm(ll a, ll b) {
  if (!a || !b)
    return 0;
  return a * b / gcd(a, b);
}
int main() {
  long long a;
  cin >> a;
  long long sum = 0;
  rep(i, a - 1) { sum += (i + 1) % (i + 2); }
  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
inline constexpr ll gcd(ll a, ll b) {
  if (!a || !b)
    return 0;
  while (b) {
    ll c = b;
    b = a % b;
    a = c;
  }
  return a;
}
inline constexpr ll lcm(ll a, ll b) {
  if (!a || !b)
    return 0;
  return a * b / gcd(a, b);
}
int main() {
  long long a;
  cin >> a;
  cout << a * (a - 1) / 2 << endl;
}
Problem p02924 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(a) (a).begin(), (a).end()
#define MOD 1000000007

int main() {
  int N;
  cin >> N;
  long long int sum = 0;
  for (int i = N; i > 1; i--) {
    sum += (i - 1 % i);
    // if(i == 1) sum += N % i;
  }
  if (N == 1)
    cout << 0;
  else
    cout << sum;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(a) (a).begin(), (a).end()
#define MOD 1000000007

int main() {
  int N;
  cin >> N;
  long long int sum = 0;
  for (int i = N; i > 1; i--)
    sum += i - 1;
  if (N == 1)
    cout << 0;
  else
    cout << sum;
}
Problem p02924 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define lli long long int
#define MODV 1000000007
#define INF INT_MAX     // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for (lli i = 0, i##_len = (lli)(n); i < i##_len; i++)
#define repf(i, n) for (lli i = 1, i##_len = (lli)(n + 1); i < i##_len; i++)
#define per(i, n) for (lli i = ((lli)(n)) - 1; i >= 0; i--)
#define perf(i, n) for (lli i = ((lli)(n)); i > 0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<lli>
#define vvi vector<vector<lli>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  lli n;
  cin >> n;
  lli sum = 0;
  repf(i, n) if (i != n) sum += i % (i + 1);
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define lli long long int
#define MODV 1000000007
#define INF INT_MAX     // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for (lli i = 0, i##_len = (lli)(n); i < i##_len; i++)
#define repf(i, n) for (lli i = 1, i##_len = (lli)(n + 1); i < i##_len; i++)
#define per(i, n) for (lli i = ((lli)(n)) - 1; i >= 0; i--)
#define perf(i, n) for (lli i = ((lli)(n)); i > 0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<lli>
#define vvi vector<vector<lli>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  lli n;
  cin >> n;
  cout << (n * (n - 1) / 2) << endl;
}
Problem p02924 (C++)

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

#define ll long long

int main() {
  ll sum = 0, ans = -1;
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    sum += (i % n);
  }
  std::cout << sum << std::endl;
  return 0;
}

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

#define ll long long

int main() {
  ll sum = 0, ans = -1;
  ll n;
  cin >> n;
  n--;
  sum = (n * (n + 1)) / 2;
  std::cout << sum << std::endl;
  return 0;
}
Problem p02924 (C++)

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

using namespace std;
using ll = long long;

int main() {
  ll N;
  cin >> N;

  vector<ll> P(N);
  vector<ll> M(N);

  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = N * (N - 1) / 2;

  cout << ans << endl;

  return 0;
}

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

using namespace std;
using ll = long long;

int main() {
  ll N;
  cin >> N;

  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = N * (N - 1) / 2;

  cout << ans << endl;

  return 0;
}
Problem p02924 (Python)

Buggy submission (status: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n = int(input())

tmp = [i for i in range(1, n + 1)]
print(sum(tmp) - n)


Fixed submission:
# -*- coding: utf-8 -*-
n = int(input())

su = n * (n + 1) // 2
print(su - n)
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
const int maxn = 1000 + 10;

int n;
int aa[maxn];
int b[maxn][maxn];
vector<int> a[maxn * maxn];
int d[maxn], deg[maxn] = {0};
int queue[maxn * maxn], front, rear;
int kq;

int id(int i, int j) { return b[i][j]; }

int main() {
  // freopen("input.txt","r",stdin);
  scanf("%d", &n);
  int nn = 0;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      nn++;
      b[i][j] = b[j][i] = nn;
    }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; j++)
      scanf("%d", &aa[j]);
    for (int j = 1; j <= n - 2; j++) {
      int u = id(i, aa[j]);
      int v = id(i, aa[j + 1]);
      // cout<<"make("<<u<<"->"<<v<<")\n";
      a[u].push_back(v);
      deg[v]++;
    }
  }
  // cout<<"NN= "<<nn<<endl;
  front = 1;
  rear = 0;
  for (int i = 1; i <= nn; i++)
    d[i] = -1;
  for (int i = 1; i <= nn; i++)
    if (deg[i] == 0) {
      rear++;
      queue[rear] = i;
      d[i] = 1;
    }
  while (front <= rear) {
    int u = queue[front];
    front++;
    for (int i = 0; i < a[u].size(); i++) {
      int v = a[u][i];
      if (deg[v] > 0) {
        deg[v]--;
        if (deg[v] == 0) {
          d[v] = d[u] + 1;
          rear++;
          queue[rear] = v;
        }
      }
    }
  }
  if (rear < nn)
    cout << -1;
  else {
    kq = 0;
    for (int i = 1; i <= nn; i++)
      kq = max(kq, d[i]);
    cout << kq;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
const int maxn = 1000 + 10;

int n;
int aa[maxn];
int b[maxn][maxn];
vector<int> a[maxn * maxn];
int d[maxn * maxn], deg[maxn * maxn] = {0};
int queue[maxn * maxn], front, rear;
int kq;

int id(int i, int j) { return b[i][j]; }

int main() {
  // freopen("input.txt","r",stdin);
  scanf("%d", &n);
  int nn = 0;
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      nn++;
      b[i][j] = b[j][i] = nn;
    }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - 1; j++)
      scanf("%d", &aa[j]);
    for (int j = 1; j <= n - 2; j++) {
      int u = id(i, aa[j]);
      int v = id(i, aa[j + 1]);
      // cout<<"make("<<u<<"->"<<v<<")\n";
      a[u].push_back(v);
      deg[v]++;
    }
  }
  // cout<<"NN= "<<nn<<endl;
  front = 1;
  rear = 0;
  for (int i = 1; i <= nn; i++)
    d[i] = -1;
  for (int i = 1; i <= nn; i++)
    if (deg[i] == 0) {
      rear++;
      queue[rear] = i;
      d[i] = 1;
    }
  while (front <= rear) {
    int u = queue[front];
    front++;
    for (int i = 0; i < a[u].size(); i++) {
      int v = a[u][i];
      if (deg[v] > 0) {
        deg[v]--;
        if (deg[v] == 0) {
          d[v] = d[u] + 1;
          rear++;
          queue[rear] = v;
        }
      }
    }
  }
  if (rear < nn)
    cout << -1;
  else {
    kq = 0;
    for (int i = 1; i <= nn; i++)
      kq = max(kq, d[i]);
    cout << kq;
  }
}
Problem p02925 (C++)

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

using namespace std;

void solve() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1));
  for (int i = 0; i < n; i++) {
    for (int j = n - 2; j >= 0; j--) {
      int x;
      cin >> x;
      a[i][j] = x - 1;
    }
  }
  int TOT = n * (n - 1) / 2;
  set<int> now;
  for (int i = 0; i < n; i++) {
    now.insert(i);
  }
  int tot = TOT;
  for (int d = 1; d <= TOT; d++) {
    int cnt = 0;
    set<int> later;
    for (int i : now) {
      if (a[i].empty() || later.find(i) != later.end())
        continue;
      int j = a[i].back();
      if (later.find(j) != later.end() || a[j].empty() || a[j].back() != i)
        continue;
      later.insert(i);
      later.insert(j);
      a[i].pop_back();
      a[j].pop_back();
      cnt++;
    }
    if (cnt == 0) {
      cout << -1;
      return;
    }
    tot -= cnt;
    if (tot == 0) {
      cout << d;
      return;
    }
  }
}

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;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(n - 1));
  for (int i = 0; i < n; i++) {
    for (int j = n - 2; j >= 0; j--) {
      int x;
      cin >> x;
      a[i][j] = x - 1;
    }
  }
  int TOT = n * (n - 1) / 2;
  set<int> now;
  for (int i = 0; i < n; i++) {
    now.insert(i);
  }
  int tot = TOT;
  for (int d = 1; d <= TOT; d++) {
    int cnt = 0;
    set<int> later;
    for (int i : now) {
      if (a[i].empty() || later.find(i) != later.end())
        continue;
      int j = a[i].back();
      if (later.find(j) != later.end() || a[j].empty() || a[j].back() != i)
        continue;
      later.insert(i);
      later.insert(j);
      a[i].pop_back();
      a[j].pop_back();
      cnt++;
    }
    if (cnt == 0) {
      cout << -1;
      return;
    }
    tot -= cnt;
    if (tot == 0) {
      cout << d;
      return;
    }
    now = later;
  }
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fs first
#define sc second
#define mk make_pair
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int INF = 1e9 + 1;
const ll LINF = 1e17;
const int MAKS = 1010;
int n, x, day, num[MAKS][MAKS], cnt, in[MAKS * MAKS];
pii pos[MAKS * MAKS];
vector<int> lst, lst2, adj[MAKS * MAKS];
bool boo[MAKS];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      num[i][j] = cnt;
      pos[cnt] = mk(i, j);
      cnt++;
    }
  }
  int last = -1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> x;
      int a = min(i, x);
      int b = max(i, x);
      int p = num[a][b];
      if (j != 0) {
        adj[last].pb(p);
        in[p]++;
      }
      last = p;
    }
  }
  for (int i = 0; i < cnt; i++) {
    if (in[i] == 0)
      lst.pb(i);
  }
  while (!lst.empty()) {
    day++;
    for (int now : lst) {
      //			printf("%d %d %d\n", pos[now].fs, pos[now].sc,
      //now);
      boo[now] = true;

      for (int next : adj[now]) {
        in[next]--;
        if (in[next] == 0)
          lst2.pb(next);
      }
    }
    lst.clear();
    for (int now : lst2)
      lst.pb(now);
    lst2.clear();
  }
  for (int i = 0; i < cnt; i++) {
    if (!boo[i]) {
      //			printf("%d\n", i);
      printf("-1");
      return 0;
    }
  }
  cout << day;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fs first
#define sc second
#define mk make_pair
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int INF = 1e9 + 1;
const ll LINF = 1e17;
const int MAKS = 1010;
int n, x, day, num[MAKS][MAKS], cnt, in[MAKS * MAKS];
pii pos[MAKS * MAKS];
vector<int> lst, lst2, adj[MAKS * MAKS];
bool boo[MAKS * MAKS];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      num[i][j] = cnt;
      pos[cnt] = mk(i, j);
      cnt++;
    }
  }
  int last = -1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n - 1; j++) {
      cin >> x;
      int a = min(i, x);
      int b = max(i, x);
      int p = num[a][b];
      if (j != 0) {
        adj[last].pb(p);
        in[p]++;
      }
      last = p;
    }
  }
  for (int i = 0; i < cnt; i++) {
    if (in[i] == 0)
      lst.pb(i);
  }
  while (!lst.empty()) {
    day++;
    for (int now : lst) {
      //			printf("%d %d %d\n", pos[now].fs, pos[now].sc,
      //now);
      boo[now] = true;

      for (int next : adj[now]) {
        in[next]--;
        if (in[next] == 0)
          lst2.pb(next);
      }
    }
    lst.clear();
    for (int now : lst2)
      lst.pb(now);
    lst2.clear();
  }
  for (int i = 0; i < cnt; i++) {
    if (!boo[i]) {
      //			printf("%d\n", i);
      printf("-1");
      return 0;
    }
  }
  cout << day;
}
Problem p02925 (C++)

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

using namespace std;

int main(void) {
  long n;
  cin >> n;

  auto idx = [&](int x, int y) {
    int t = min(x, y);
    int s = max(x, y);
    return t * n + s;
  };

  vector<vector<long>> a(n, vector<long>(n - 1));
  for (auto &x : a)
    for (auto &y : x)
      cin >> y, y--;

  vector<int> out(n * n);
  vector<vector<int>> G(n * n), rG(n * n);
  vector<long> dis(n * n);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n - 1 - 1; j++) {
      int s = idx(i, a[i][j]);
      int t = idx(i, a[i][j + 1]);
      G[s].push_back(t);
      rG[t].push_back(s);
      out[s]++;
    }

  queue<int> q;
  for (int i = 0; i < n * n; i++)
    if (!out[i])
      q.push(i);

  while (!q.empty()) {
    int i = q.front();
    q.pop();

    long r = -1;
    for (int f : G[i])
      r = max(r, dis[f]);
    dis[i] = r + 1;

    for (int p : rG[i])
      if (!--out[p])
        q.push(p);
  }

  for (int i = 0; i < n * n; i++)
    if (out[i]) {
      cout << -1 << endl;
      return 1;
    }

  long r = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      r = max(r, dis[idx(i, j)]);
  cout << r + 1 << endl;
  return 0;
}


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

using namespace std;

int main(void) {
  long n;
  cin >> n;

  auto idx = [&](int x, int y) {
    int t = min(x, y);
    int s = max(x, y);
    return t * n + s;
  };

  vector<vector<long>> a(n, vector<long>(n - 1));
  for (auto &x : a)
    for (auto &y : x)
      cin >> y, y--;

  vector<int> out(n * n);
  vector<vector<int>> G(n * n), rG(n * n);
  vector<long> dis(n * n);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n - 1 - 1; j++) {
      int s = idx(i, a[i][j]);
      int t = idx(i, a[i][j + 1]);
      G[s].push_back(t);
      rG[t].push_back(s);
      out[s]++;
    }

  queue<int> q;
  for (int i = 0; i < n * n; i++)
    if (!out[i])
      q.push(i);

  while (!q.empty()) {
    int i = q.front();
    q.pop();

    long r = -1;
    for (int f : G[i])
      r = max(r, dis[f]);
    dis[i] = r + 1;

    for (int p : rG[i])
      if (!--out[p])
        q.push(p);
  }

  for (int i = 0; i < n * n; i++)
    if (out[i]) {
      cout << -1 << endl;
      return 0;
    }

  long r = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      r = max(r, dis[idx(i, j)]);
  cout << r + 1 << endl;
  return 0;
}
Problem p02925 (C++)

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

queue<int> q[1000];
int n, a, i, j;
int d[1000];
int c = 0, t = 0, now, l;
bool f;

int main() {
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n - 1; j++) {
      scanf("%d", &a);
      q[i].push(a - 1);
    }
    d[i] = -1;
  }
  l = n * (n - 1) / 2;

  while (true) {
    f = false;
    for (i = 0; i < n - 1; i++) {
      if (!q[i].empty()) {
        now = q[i].front();
        if (d[i] != c && d[now] != c && !q[now].empty() &&
            q[now].front() == i) {
          q[i].pop(), q[now].pop();
          d[i] = c, d[now] = c;
          f = true;
          t++;
        }
      }
    }
    c++;
    if (!f) {
      cout << "-1" << endl;
      return 0;
    }
    if (t == l) {
      cout << c << endl;
      return 0;
    }
  }
}


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

queue<int> q[1000];
int n, a, i, j;
int d[1000];
int c = 0, t = 0, now, l;
bool f;

int main() {
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n - 1; j++) {
      scanf("%d", &a);
      q[i].push(a - 1);
    }
    d[i] = -1;
  }
  l = n * (n - 1) / 2;

  while (true) {
    f = false;
    for (i = 0; i < n - 1; i++) {
      if (!q[i].empty()) {
        now = q[i].front();
        if (!q[now].empty() && q[now].front() == i) {
          if (d[i] == c || d[now] == c)
            continue;
          q[i].pop(), q[now].pop();
          d[i] = c, d[now] = c;
          f = true;
          t++;
        }
      }
    }
    c++;
    if (!f) {
      cout << "-1" << endl;
      return 0;
    }
    if (t == l) {
      cout << c << endl;
      return 0;
    }
  }
}
Problem p02925 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f, maxn = 1e3 + 1;
int a[maxn][maxn], nowp[maxn], vis[maxn], n;
int main() {
  int i, j, k, ans = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    nowp[i] = 1;
    for (j = 1; j <= n - 1; j++)
      cin >> a[i][j];
  }
  int cnt = 1;
  while (1) {
    bool f = false;
    for (i = 1; i <= n; i++) {
      if (nowp[i] >= n || vis[i] == cnt)
        continue;
      j = a[i][nowp[i]];
      if (vis[j] != cnt && nowp[j] < n && a[j][nowp[j]] == i) {
        nowp[j]++;
        nowp[i]++;
        vis[i] = vis[j] = cnt;
        f = true;
      }
    }
    if (!f)
      break;
    cnt++;
  }
  for (int i = 1; i <= n; i++) {
    if (nowp[i] != n) {
      cout << -1;
      return 0;
    }
  }
  cout << cnt - 1;
  return 0;
}

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f, maxn = 1e3 + 50;
int a[maxn][maxn], nowp[maxn], vis[maxn], n;
int main() {
  int i, j, k, ans = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    nowp[i] = 1;
    for (j = 1; j <= n - 1; j++)
      cin >> a[i][j];
  }
  int cnt = 1;
  while (1) {
    bool f = false;
    for (i = 1; i <= n; i++) {
      if (nowp[i] >= n || vis[i] == cnt)
        continue;
      j = a[i][nowp[i]];
      if (vis[j] != cnt && nowp[j] < n && a[j][nowp[j]] == i) {
        nowp[j]++;
        nowp[i]++;
        vis[i] = vis[j] = cnt;
        f = true;
      }
    }
    if (!f)
      break;
    cnt++;
  }
  for (int i = 1; i <= n; i++) {
    if (nowp[i] != n) {
      cout << -1;
      return 0;
    }
  }
  cout << cnt - 1;
  return 0;
}
Problem p02925 (C++)

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

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define rep(i, n) for (int i = 0; i < (n); i++)

template <class T, class U> using umap = unordered_map<T, U>;
using ll = long long;

ll n;
vector<vector<ll>> a;
umap<ll, vector<ll>> to;
int main() {
  const ll MAXN = 1010;
  const ll MAXV = MAXN * MAXN * 0.5;
  a.resize(n, vector<ll>(n - 1));
  ll tbl[MAXN][MAXN] = {};
  ll v = 0;
  rep(i, n) {
    rep(j, n) {
      if (i < j)
        tbl[i][j] = v++;
    }
  }
  auto id = [&](ll i, ll j) {
    if (i > j)
      swap(i, j);
    return tbl[i][j];
  };
  rep(i, n) {
    rep(j, n - 1) {
      cin >> a[i][j];
      a[i][j]--;
      if (j)
        to[id(i, a[i][j - 1])].push_back(id(i, a[i][j]));
    }
  }

  ll dist[MAXV] = {};
  set<ll> vst, fin;
  auto dfs = [&](ll i, auto func) -> ll {
    if (vst.count(i) == 1) {
      if (fin.count(i) == 0)
        return -1;
      return dist[i];
    }
    vst.insert(i);
    ll ret = 1;
    for (auto &e : to[i]) {
      ll d = func(e, func);
      if (d == -1)
        return -1;
      else
        ret = max(ret, d + 1);
    }
    fin.insert(i);
    dist[i] = ret;
    return dist[i];
  };
  ll ans = 1;
  rep(i, MAXV) {
    ll val = dfs(i, dfs);
    if (val == -1) {
      ans = -1;
      break;
    }
    ans = max(ans, val);
  }
  cout << ans << endl;
  return 0;
}


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

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define rep(i, n) for (int i = 0; i < (n); i++)

template <class T, class U> using umap = unordered_map<T, U>;
using ll = long long;

ll n;
vector<vector<ll>> a;
umap<ll, vector<ll>> to;
int main() {
  cin >> n;
  const ll MAXN = 1010;
  const ll MAXV = MAXN * MAXN * 0.5;
  a.resize(n, vector<ll>(n - 1));
  ll tbl[MAXN][MAXN] = {};
  ll v = 0;
  rep(i, n) {
    rep(j, n) {
      if (i < j)
        tbl[i][j] = v++;
    }
  }
  auto id = [&](ll i, ll j) {
    if (i > j)
      swap(i, j);
    return tbl[i][j];
  };
  rep(i, n) {
    rep(j, n - 1) {
      cin >> a[i][j];
      a[i][j]--;
      if (j)
        to[id(i, a[i][j - 1])].push_back(id(i, a[i][j]));
    }
  }

  ll dist[MAXV] = {};
  set<ll> vst, fin;
  auto dfs = [&](ll i, auto func) -> ll {
    if (vst.count(i) == 1) {
      if (fin.count(i) == 0)
        return -1;
      return dist[i];
    }
    vst.insert(i);
    ll ret = 1;
    for (auto &e : to[i]) {
      ll d = func(e, func);
      if (d == -1)
        return -1;
      else
        ret = max(ret, d + 1);
    }
    fin.insert(i);
    dist[i] = ret;
    return dist[i];
  };
  ll ans = 1;
  rep(i, MAXV) {
    ll val = dfs(i, dfs);
    if (val == -1) {
      ans = -1;
      break;
    }
    ans = max(ans, val);
  }
  cout << ans << endl;
  return 0;
}
Problem p02925 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;

vector<int> edge[500000];
int deg[500000];
int d[500000];

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 2; j++) {
      int from = max(i, a[i][j]) * n + min(i, a[i][j]);
      int to = max(i, a[i][j + 1]) * n + min(i, a[i][j + 1]);
      edge[from].push_back(to);
      deg[to]++;
    }
  }
  queue<int> q;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (i == j)
        continue;
      int id = max(i, j) * n + min(i, j);
      if (deg[id] == 0) {
        q.push(id);
        d[id] = 1;
      }
    }
  }
  vector<int> toposo;
  int ans = -1;
  while (q.size()) {
    int x = q.front();
    q.pop();
    toposo.push_back(x);
    ans = max(ans, d[x]);
    for (int y : edge[x]) {
      deg[y]--;
      if (deg[y] == 0) {
        q.push(y);
        d[y] = d[x] + 1;
      }
    }
  }
  if (toposo.size() != n * (n - 1) / 2) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;

vector<int> edge[1000000];
int deg[1000000];
int d[1000000];

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 2; j++) {
      int from = max(i, a[i][j]) * n + min(i, a[i][j]);
      int to = max(i, a[i][j + 1]) * n + min(i, a[i][j + 1]);
      edge[from].push_back(to);
      deg[to]++;
    }
  }
  queue<int> q;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (i == j)
        continue;
      int id = max(i, j) * n + min(i, j);
      if (deg[id] == 0) {
        q.push(id);
        d[id] = 1;
      }
    }
  }
  vector<int> toposo;
  int ans = -1;
  while (q.size()) {
    int x = q.front();
    q.pop();
    toposo.push_back(x);
    ans = max(ans, d[x]);
    for (int y : edge[x]) {
      deg[y]--;
      if (deg[y] == 0) {
        q.push(y);
        d[y] = d[x] + 1;
      }
    }
  }
  if (toposo.size() != n * (n - 1) / 2) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}