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

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MD = 1000000007;

/* sample input

*/

int main() {
  ll n, k;
  vector<ll> a;

  cin >> n >> k;
  a.resize(n);

  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == k) {
      cout << "POSSIBLE";
      return 0;
    }
  }

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

  if (a[n - 1] < k) {
    cout << "IMPOSSIBLE";
    return 0;
  }

  ll minsub;
  bool flug = true;
  while (flug) {
    minsub = LONG_MAX;
    ll length = a.size();
    for (ll i = 0; i < length - 1; i++) {
      minsub = min(minsub, abs(a[i + 1] - a[i]));
    }
    if (minsub == k) {
      cout << "POSSIBLE";
      return 0;
    }

    for (ll i = 0; i < length; i++) {
      if (a[i] == minsub) {
        flug = false;
        break;
      };
    }
    if (flug) {
      a.push_back(minsub);
      sort(a.begin(), a.end());
    }
  }

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

  if (a[0] == 1) {
    cout << "POSSIBLE";
  } else {
    ll length = a.size();
    if (k % a[0] == 0 && a[length - 1] != a[0]) {
      cout << "POSSIBLE";
      return 0;
    } else {
      cout << "IMPOSSIBLE";
      return 0;
    }
  }

  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MD = 1000000007;

/* sample input

*/

int main() {
  ll n, k;
  vector<ll> a;

  cin >> n >> k;
  a.resize(n);

  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == k) {
      cout << "POSSIBLE";
      return 0;
    }
  }

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

  if (a[n - 1] < k) {
    cout << "IMPOSSIBLE";
    return 0;
  }

  ll minsub;
  bool flug = true;
  while (flug) {
    minsub = LONG_MAX;
    ll length = a.size();
    for (ll i = 0; i < length - 1; i++) {
      if (abs(a[i + 1] - a[i]) > 0) {
        minsub = min(minsub, abs(a[i + 1] - a[i]));
      }
    }
    if (minsub == k) {
      cout << "POSSIBLE";
      return 0;
    }

    for (ll i = 0; i < length; i++) {
      if (a[i] == minsub) {
        flug = false;
        break;
      };
    }
    if (flug) {
      a.push_back(minsub);
      sort(a.begin(), a.end());
    }
  }

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

  if (a[0] == 1) {
    cout << "POSSIBLE";
  } else {
    ll length = a.size();
    if (k % a[0] == 0 && a[length - 1] != a[0]) {
      cout << "POSSIBLE";
      return 0;
    } else {
      cout << "IMPOSSIBLE";
      return 0;
    }
  }

  return 0;
}
Problem p03651 (C++)

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

inline int gcd(int x, int y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}

int main() {
  cin.tie(0)->sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  REP(i, n) cin >> a[i];

  int g = gcd(a[0], a[1]);
  REP(i, n - 1)
  g = gcd(g, a[i + 1]);

  if (k <= *max_element(a.begin(), a.end()) && k % g == 0)
    cout << "POSSIBLE" << '\n';
  else
    cout << "IMPOSSIBLE" << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

inline int gcd(int x, int y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}

int main() {
  cin.tie(0)->sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  REP(i, n) cin >> a[i];

  int g = a[0];
  REP(i, n) g = gcd(g, a[i]);

  if (k <= *max_element(a.begin(), a.end()) && k % g == 0)
    cout << "POSSIBLE" << '\n';
  else
    cout << "IMPOSSIBLE" << '\n';
  return 0;
}
Problem p03652 (C++)

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

const int N = 105;

int n, m, num, ans, p, mx, mn;
int a[N][N], cnt[N];
bool vis[N];

int main() {
  int i, j;
  scanf("%d%d", &n, &m);
  mn = n;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      scanf("%d", &a[i][j]);
  for (i = 1; i <= m; i++)
    vis[i] = 1;
  while (1) {
    memset(cnt, 0, sizeof(cnt));
    ans = 0;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++)
        if (vis[a[i][j]]) {
          cnt[a[i][j]]++;
          ans++;
          break;
        }
    if (ans < n)
      break;
    mx = 0;
    for (i = 1; i <= m; i++)
      if (cnt[i] > mx && vis[i]) {
        mx = cnt[i];
        p = i;
      }
    vis[p] = 0;
    mn = min(mn, mx);
  }
  printf("%d\n", mn);
  return 0;
}

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

const int N = 305;

int n, m, num, ans, p, mx, mn;
int a[N][N], cnt[N];
bool vis[N];

int main() {
  int i, j;
  scanf("%d%d", &n, &m);
  mn = n;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      scanf("%d", &a[i][j]);
  for (i = 1; i <= m; i++)
    vis[i] = 1;
  while (1) {
    memset(cnt, 0, sizeof(cnt));
    ans = 0;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++)
        if (vis[a[i][j]]) {
          cnt[a[i][j]]++;
          ans++;
          break;
        }
    if (ans < n)
      break;
    mx = 0;
    for (i = 1; i <= m; i++)
      if (cnt[i] > mx && vis[i]) {
        mx = cnt[i];
        p = i;
      }
    vis[p] = 0;
    mn = min(mn, mx);
  }
  printf("%d\n", mn);
  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
int n, m;
int a[400][400];
int v[400], vv[400];
stack<int> st[305];

int main() {
  int i, j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      scanf("%d", &a[i][j]);
  for (i = 1; i <= n; i++) {
    for (j = m; j >= 1; j--)
      st[i].push(a[i][j]);
  }
  int x = 0, maxnum;
  memset(vv, 0, sizeof(vv));
  for (j = 1; j <= n; j++)
    vv[st[j].top()]++;
  for (j = 1; j <= m; j++)
    if (vv[j] > x) {
      x = vv[j];
      maxnum = j;
    }
  int ans = x;
  for (i = 1; i <= n - 1; i++) {
    v[maxnum] = 1;
    for (j = 1; j <= n; j++) {
      while (v[st[j].top()] == 1)
        st[j].pop();
    }
    x = 0;
    memset(vv, 0, sizeof(vv));
    for (j = 1; j <= n; j++)
      vv[st[j].top()]++;
    for (j = 1; j <= m; j++)
      if (vv[j] > x) {
        x = vv[j];
        maxnum = j;
      }
    ans = min(ans, x);
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
int n, m;
int a[400][400];
int v[400], vv[400];
stack<int> st[305];

int main() {
  int i, j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      scanf("%d", &a[i][j]);
  for (i = 1; i <= n; i++) {
    for (j = m; j >= 1; j--)
      st[i].push(a[i][j]);
  }
  int x = 0, maxnum;
  memset(vv, 0, sizeof(vv));
  for (j = 1; j <= n; j++)
    vv[st[j].top()]++;
  for (j = 1; j <= m; j++)
    if (vv[j] > x) {
      x = vv[j];
      maxnum = j;
    }
  int ans = x;
  for (i = 1; i <= m - 1; i++) {
    v[maxnum] = 1;
    for (j = 1; j <= n; j++) {
      while (v[st[j].top()] == 1)
        st[j].pop();
    }
    x = 0;
    memset(vv, 0, sizeof(vv));
    for (j = 1; j <= n; j++)
      vv[st[j].top()]++;
    for (j = 1; j <= m; j++)
      if (vv[j] > x) {
        x = vv[j];
        maxnum = j;
      }
    ans = min(ans, x);
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03652 (C++)

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

int n, m, ans = 2e9;
int a[305][305];
int f[305];
bool v[305];

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

  for (int i = 1; i <= m; i++)
    v[i] = 1;

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

  int fmax, vmax;
  for (int k = 0; m / (n - k) < ans; k++) {
    fmax = vmax = 0;

    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        if (v[a[i][j]]) {
          f[a[i][j]]++;
          if (f[a[i][j]] > fmax) {
            fmax = f[a[i][j]];
            vmax = a[i][j];
          }
          break;
        }

    memset(f, 0x00, sizeof f);

    ans = min(ans, fmax);
    v[vmax] = 0;
  }

  cout << ans << '\n';

  return 0;
}

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

int n, m, ans = 2e9;
int a[305][305];
int f[305];
bool v[305];

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

  for (int i = 1; i <= m; i++)
    v[i] = 1;

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

  int fmax, vmax;
  for (int k = 0; k < m; k++) {
    fmax = vmax = 0;

    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        if (v[a[i][j]]) {
          f[a[i][j]]++;
          if (f[a[i][j]] > fmax) {
            fmax = f[a[i][j]];
            vmax = a[i][j];
          }
          break;
        }

    memset(f, 0x00, sizeof f);

    ans = min(ans, fmax);
    v[vmax] = 0;
  }

  cout << ans << '\n';

  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define M_PI 3.14159265358979323846
#define FOR(i, l, r) for (int i = l; i <= r; i++)
#define LOR(i, l, r) for (ll i = l; i <= r; i++)
#define FORD(i, r, l) for (int i = r; i >= l; i--)
#define LORD(i, r, l) for (ll i = r; i >= l; i--)
#define INF 1000000000
#define CL(x) memset(x, 0, sizeof(x))
#define DEB(x) cout << #x << " : " << x << '\n'
#define ALL(x) x.begin(), x.end()
#define SZ(x) x.size()
#define UI(x) (int)(x - 'A')
#define LI(x) (int)(x - 'a')
typedef long long ll;

#define MXN 305
int N, M;
int A[MXN][MXN];
bool de[MXN];
int Ans;
int now[MXN];
int cnt[MXN];

/*
greedy with good amortized complexity
*/

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

  cin >> N >> M;
  FOR(i, 1, N) {
    FOR(j, 1, M) cin >> A[i][j];
    now[i] = 1;
  }
  Ans = N;
  FOR(c, 1, N - 1) {
    FOR(i, 1, M) cnt[i] = 0;
    int mx = 0, mxi;
    FOR(i, 1, N) {
      int ni = A[i][now[i]];
      cnt[ni]++;
      if (cnt[ni] > mx) {
        mx = cnt[ni];
        mxi = ni;
      }
    }
    Ans = min(Ans, mx);
    de[mxi] = true;
    FOR(i, 1, N) {
      while (de[A[i][now[i]]])
        now[i]++;
    }
  }
  cout << Ans << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define pdd pair<double, double>
#define f first
#define s second
#define MOD 1000000007
#define mkp make_pair
#define M_PI 3.14159265358979323846
#define FOR(i, l, r) for (int i = l; i <= r; i++)
#define LOR(i, l, r) for (ll i = l; i <= r; i++)
#define FORD(i, r, l) for (int i = r; i >= l; i--)
#define LORD(i, r, l) for (ll i = r; i >= l; i--)
#define INF 1000000000
#define CL(x) memset(x, 0, sizeof(x))
#define DEB(x) cout << #x << " : " << x << '\n'
#define ALL(x) x.begin(), x.end()
#define SZ(x) x.size()
#define UI(x) (int)(x - 'A')
#define LI(x) (int)(x - 'a')
typedef long long ll;

#define MXN 305
int N, M;
int A[MXN][MXN];
bool de[MXN];
int Ans;
int now[MXN];
int cnt[MXN];

/*
greedy with good amortized complexity
*/

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

  cin >> N >> M;
  FOR(i, 1, N) {
    FOR(j, 1, M) cin >> A[i][j];
    now[i] = 1;
  }
  Ans = N;

  FOR(c, 1, M - 1) {
    FOR(i, 1, M) cnt[i] = 0;
    int mx = 0, mxi;
    FOR(i, 1, N) {
      int ni = A[i][now[i]];
      cnt[ni]++;
      if (cnt[ni] > mx) {
        mx = cnt[ni];
        mxi = ni;
      }
    }
    Ans = min(Ans, mx);
    de[mxi] = true;
    FOR(i, 1, N) {
      while (de[A[i][now[i]]])
        now[i]++;
    }
  }
  cout << Ans << '\n';

  return 0;
}
Problem p03652 (C++)

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

int sports[301][301];
int main() {
  int n, m, a;

  cin >> n >> m;

  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      cin >> sports[i][j];

  for (int i = 1; i <= n; ++i) {
    vector<int> cur_amount(301), cur_loc(301), prev_loc(301);
    vector<bool> not_allowed(301);

    for (int k = 0; k <= m; ++k) {
      for (int j = 0; j < n; ++j) {
        while (not_allowed[sports[j][cur_loc[j]]])
          ++cur_loc[j];

        if (cur_loc[j] >= n)
          goto stop;

        if (cur_loc[j] != prev_loc[j] || k == 0)
          ++cur_amount[sports[j][cur_loc[j]]];

        if (cur_amount[sports[j][cur_loc[j]]] > i) {
          not_allowed[sports[j][cur_loc[j]]] = true;
        }

        prev_loc[j] = cur_loc[j];
      }
    }

    cout << i << '\n';
    return 0;

  stop:
    int x = 0;
  }
}

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

int sports[301][301];
int main() {
  int n, m, a;

  cin >> n >> m;

  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      cin >> sports[i][j];

  for (int i = 1; i <= n; ++i) {
    vector<int> cur_amount(301), cur_loc(301), prev_loc(301);
    vector<bool> not_allowed(301);

    for (int k = 0; k <= m; ++k) {
      for (int j = 0; j < n; ++j) {
        while (not_allowed[sports[j][cur_loc[j]]])
          ++cur_loc[j];

        if (cur_loc[j] >= m)
          goto stop;

        if (cur_loc[j] != prev_loc[j] || k == 0)
          ++cur_amount[sports[j][cur_loc[j]]];

        if (cur_amount[sports[j][cur_loc[j]]] > i) {
          not_allowed[sports[j][cur_loc[j]]] = true;
        }

        prev_loc[j] = cur_loc[j];
      }
    }

    cout << i << '\n';
    return 0;

  stop:
    int x = 0;
  }
}
Problem p03652 (C++)

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

typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 2003;

int main() {

  int N, M;
  cin >> N >> M;
  int A[N + 1][M + 1], now[N + 1]; // now[i]はiは何番目にいるかをみてる
  bool B[M + 1];                   // B[i]=1ならまだiは使える
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < M + 1; j++) {
      if (i && j)
        cin >> A[i][j];
      else
        A[i][j] = -1;
      B[j] = 1;
    }
    now[i] = 1;
  }

  int ans = N;

  for (int j = 0; j < M; j++) {
    vector<int> cnt(M + 1);
    for (int i = 0; i <= N; i++) {
      cnt[i] = 0;
    }
    for (int i = 1; i <= N; i++) {
      while (B[A[i][now[i]]] == 0) {
        now[i]++;
      }
      cnt[A[i][now[i]]]++;
    }
    int Q = 0, p = 0;
    for (int i = 1; i <= M; i++) {
      if (cnt[i] > Q) {
        Q = cnt[i];
        p = i;
      }
    }
    ans = min(ans, Q);
    B[p] = 0;
  }
  /*for(int i=0;i<=N;i++){
      if(B[i]) cout<<i<<endl;
  }*/
  cout << ans << endl;
}


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

typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int mod = 1000000007, MAX = 2003;

int main() {

  int N, M;
  cin >> N >> M;
  int A[N + 1][M + 1], now[N + 1]; // now[i]はiは何番目にいるかをみてる
  bool B[M + 1];                   // B[i]=1ならまだiは使える
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < M + 1; j++) {
      if (i && j)
        cin >> A[i][j];
      else
        A[i][j] = -1;
      B[j] = 1;
    }
    now[i] = 1;
  }

  int ans = N;

  for (int j = 0; j < M; j++) {
    vector<int> cnt(M + 1);
    for (int i = 0; i <= M; i++) {
      cnt[i] = 0;
    }
    for (int i = 1; i <= N; i++) {
      while (B[A[i][now[i]]] == 0) {
        now[i]++;
      }
      cnt[A[i][now[i]]]++;
    }
    int Q = 0, p = 0;
    for (int i = 1; i <= M; i++) {
      if (cnt[i] > Q) {
        Q = cnt[i];
        p = i;
      }
    }
    ans = min(ans, Q);
    B[p] = 0;
  }
  /*for(int i=0;i<=N;i++){
      if(B[i]) cout<<i<<endl;
  }*/
  cout << ans << endl;
}
Problem p03652 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define REPL(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define repl(i, n) REPL(i, 0, n)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int N, M;
vector<int> id;
vector<vector<int>> A;
unordered_set<int> se;
int ans = inf;
void dfs() {
  map<int, int> mp;
  rep(i, N) mp[A[i][id[i]]]++;
  int mx = 0;
  for (auto m : mp)
    mx = max(m.second, mx);
  ans = min(mx, ans);
  if (se.size() == 1)
    return;
  vector<int> tmpId(N);
  rep(i, N) tmpId[i] = id[i];
  for (auto m : mp) {
    if (m.second == mx) {
      se.erase(m.first);
      rep(i, N) while (id[i] < M && !se.count(A[i][id[i]])) id[i]++;
      dfs();
      se.insert(m.first);
      rep(i, N) id[i] = tmpId[i];
    }
  }
}
int main() {
  cin >> N >> M;
  A = vector<vector<int>>(N, vector<int>(M));
  rep(i, N) rep(j, M) cin >> A[i][j];
  id = vector<int>(N);
  rep(i, M) se.insert(i + 1);
  dfs();
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define REPL(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define repl(i, n) REPL(i, 0, n)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int N, M;
vector<int> id;
vector<vector<int>> A;
unordered_set<int> se;
int ans = inf;
void dfs() {
  map<int, int> mp;
  rep(i, N) mp[A[i][id[i]]]++;
  int mx = 0;
  for (auto m : mp)
    mx = max(m.second, mx);
  ans = min(mx, ans);
  if (se.size() == 1)
    return;
  vector<int> tmpId(N);
  rep(i, N) tmpId[i] = id[i];
  for (auto m : mp) {
    if (m.second == mx) {
      se.erase(m.first);
      rep(i, N) while (id[i] < M && !se.count(A[i][id[i]])) id[i]++;
      dfs();
      break;
      se.insert(m.first);
      rep(i, N) id[i] = tmpId[i];
    }
  }
}
int main() {
  cin >> N >> M;
  A = vector<vector<int>>(N, vector<int>(M));
  rep(i, N) rep(j, M) cin >> A[i][j];
  id = vector<int>(N);
  rep(i, M) se.insert(i + 1);
  dfs();
  cout << ans << endl;
  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
int main() {
  ll n, m;
  std::cin >> n >> m;
  vector<vector<ll>> a(n, vector<ll>(m));

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

  ll ans = 400;

  vector<ll> now(n, 0);

  ll rem = m;

  vector<bool> doo(m, true);

  while (rem > 0) {
    map<ll, ll> ma;
    for (int j = 0; j < n; j++) {
      ma[a[j][now[j]]]++;
    }

    ll tmp_max = 0;

    for (auto e : ma) {
      tmp_max = max(tmp_max, e.second);
    }

    ans = min(ans, tmp_max);

    for (auto e : ma) {
      if (e.second == tmp_max) {
        rem--;
        doo[e.first] = false;
      }
    }

    for (int i = 0; i < n; i++) {
      while (!doo[a[i][now[i]]]) {
        now[i]++;
      }
    }
  }

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

Fixed submission:
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
int main() {
  ll n, m;
  std::cin >> n >> m;
  vector<vector<ll>> a(n, vector<ll>(m));

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

  ll ans = 400;

  vector<ll> now(n, 0);

  ll rem = m;

  vector<bool> doo(m, true);

  while (rem > 0) {
    map<ll, ll> ma;
    for (int j = 0; j < n; j++) {
      ma[a[j][now[j]]]++;
    }

    ll tmp_max = 0;

    for (auto e : ma) {
      tmp_max = max(tmp_max, e.second);
    }

    ans = min(ans, tmp_max);

    for (auto e : ma) {
      if (e.second == tmp_max) {
        rem--;
        doo[e.first] = false;
      }
    }

    for (int i = 0; i < n; i++) {
      while (!doo[a[i][now[i]]]) {
        now[i]++;
        if (now[i] == m)
          break;
      }
    }
  }

  std::cout << ans << std::endl;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<queue<int>> q(n);
  rep(i, n) {
    rep(j, m) {
      int a;
      cin >> a;
      q[i].push(a);
    }
  }

  int res = 400;
  set<int> st;
  rep(I, n) {
    map<int, int> mp;
    rep(i, n) {
      while (st.find(q[i].front()) != st.end()) {
        q[i].pop();
      }
      mp[q[i].front()]++;
    }
    int mx = 0;
    int nax;
    for (auto it = mp.begin(); it != mp.end(); ++it) {
      if (mx < it->second) {
        mx = it->second;
        nax = it->first;
      }
    }
    res = min(res, mx);
    st.insert(nax);
  }
  cout << res << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<queue<int>> q(n);
  rep(i, n) {
    rep(j, m) {
      int a;
      cin >> a;
      q[i].push(a);
    }
  }

  int res = 400;
  set<int> st;
  rep(I, m) {
    map<int, int> mp;
    rep(i, n) {
      while (st.find(q[i].front()) != st.end()) {
        q[i].pop();
      }
      mp[q[i].front()]++;
    }
    int mx = 0;
    int nax;
    for (auto it = mp.begin(); it != mp.end(); ++it) {
      if (mx < it->second) {
        mx = it->second;
        nax = it->first;
      }
    }
    res = min(res, mx);
    st.insert(nax);
  }
  cout << res << "\n";
  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, m, a[500][500], f[500], flag[500], ans;
bool fk[500], ff;
int read() {
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  int kk = 1, k = 0;
  if (c == '-')
    kk = -1, c = getchar();
  while (c >= '0' && c <= '9')
    k = k * 10 + c - '0', c = getchar();
  return kk * k;
}
signed main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      a[i][j] = read();
  for (int i = 1; i <= n; i++)
    f[i] = 1, flag[a[i][1]]++;
  ff = 1;
  ans = 1e9;
  while (ff) {
    int k = 0;
    for (int i = 1; i <= m; i++)
      if (flag[i] > flag[k])
        k = i;
    ans = min(ans, flag[k]);
    fk[k] = 1;
    for (int i = 1; i <= n; i++) {
      while (fk[a[i][f[i]]])
        flag[a[i][f[i]]]--, f[i]++, flag[a[i][f[i]]]++;
      ff &= f[i] != n + 1;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, a[500][500], f[500], flag[500], ans;
bool fk[500], ff;
int read() {
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  int kk = 1, k = 0;
  if (c == '-')
    kk = -1, c = getchar();
  while (c >= '0' && c <= '9')
    k = k * 10 + c - '0', c = getchar();
  return kk * k;
}
signed main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      a[i][j] = read();
  for (int i = 1; i <= n; i++)
    f[i] = 1, flag[a[i][1]]++;
  ff = 1;
  ans = 1e9;
  while (ff) {
    int k = 0;
    for (int i = 1; i <= m; i++)
      if (flag[i] > flag[k])
        k = i;
    ans = min(ans, flag[k]);
    fk[k] = 1;
    for (int i = 1; i <= n; i++) {
      while (fk[a[i][f[i]]])
        flag[a[i][f[i]]]--, f[i]++, flag[a[i][f[i]]]++;
      ff &= f[i] != m + 1;
    }
  }
  cout << ans << endl;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#define PB push_back
#define MP make_pair
#define sz(v) (in((v).size()))
#define forn(i, n) for (in i = 0; i < (n); ++i)
#define forv(i, v) forn(i, sz(v))
#define fors(i, s) for (auto i = (s).begin(); i != (s).end(); ++i)
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef long long in;
typedef vector<in> VI;
typedef vector<VI> VVI;
in n, m;
in bst = 1e9;
VVI pm;
VI nxt;
VI act;
VI pct;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  pm.resize(n, VI(m));
  nxt.resize(n, 0);
  forn(i, n) {
    forn(j, m) {
      cin >> pm[i][j];
      --pm[i][j];
    }
  }
  act.resize(m, 1);
  pct.resize(m, 0);
  forn(i, n)++ pct[pm[i][0]];
  in mx = -1;
  in argmx = -1;
  forn(i, m) {
    if (pct[i] > mx) {
      mx = pct[i];
      argmx = i;
    }
  }
  bst = min(bst, mx);
  forn(z, n - 1) {
    act[argmx] = 0;
    forn(i, n) {
      while (!act[pm[i][nxt[i]]])
        ++nxt[i];
    }
    forn(i, m) pct[i] = 0;
    forn(i, n)++ pct[pm[i][nxt[i]]];
    mx = -1;
    argmx = -1;
    forn(i, m) {
      if (pct[i] > mx) {
        mx = pct[i];
        argmx = i;
      }
    }
    bst = min(bst, mx);
  }
  cout << bst << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#define PB push_back
#define MP make_pair
#define sz(v) (in((v).size()))
#define forn(i, n) for (in i = 0; i < (n); ++i)
#define forv(i, v) forn(i, sz(v))
#define fors(i, s) for (auto i = (s).begin(); i != (s).end(); ++i)
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef long long in;
typedef vector<in> VI;
typedef vector<VI> VVI;
in n, m;
in bst = 1e9;
VVI pm;
VI nxt;
VI act;
VI pct;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  pm.resize(n, VI(m));
  nxt.resize(n, 0);
  forn(i, n) {
    forn(j, m) {
      cin >> pm[i][j];
      --pm[i][j];
    }
  }
  act.resize(m, 1);
  pct.resize(m, 0);
  forn(i, n)++ pct[pm[i][0]];
  in mx = -1;
  in argmx = -1;
  forn(i, m) {
    if (pct[i] > mx) {
      mx = pct[i];
      argmx = i;
    }
  }
  bst = min(bst, mx);
  forn(z, m - 1) {
    act[argmx] = 0;
    forn(i, n) {
      while (!act[pm[i][nxt[i]]])
        ++nxt[i];
    }
    forn(i, m) pct[i] = 0;
    forn(i, n)++ pct[pm[i][nxt[i]]];
    mx = -1;
    argmx = -1;
    forn(i, m) {
      if (pct[i] > mx) {
        mx = pct[i];
        argmx = i;
      }
    }
    bst = min(bst, mx);
  }
  cout << bst << endl;
  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stdio.h>

using namespace std;
const int maxn = 305;

int ans = 1 << 30, p[maxn], usd[maxn], a[maxn][maxn], n, m, cnt[maxn];
int main() {
#ifdef Amberframe
  freopen("agc018b.in", "r", stdin);
  freopen("agc018b.out", "w", stdout);
#endif
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", &a[i][j]);
  for (int i = 1; i <= n; i++)
    p[i] = 1;

  for (int i = 1; i <= n; i++) {
    int o = 0;
    memset(cnt, 0, sizeof cnt);
    for (int j = 1; j <= n; j++) {
      while (usd[a[j][p[j]]])
        ++p[j];
      int t = a[j][p[j]];
      if (++cnt[t] > cnt[o])
        o = t;
    }
    ans = min(ans, cnt[o]);
    usd[o] = 1;
  }
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stdio.h>

using namespace std;
const int maxn = 305;

int ans = 1 << 30, p[maxn], usd[maxn], a[maxn][maxn], n, m, cnt[maxn];
int main() {
#ifdef Amberframe
  freopen("agc018b.in", "r", stdin);
  freopen("agc018b.out", "w", stdout);
#endif
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", &a[i][j]);
  for (int i = 1; i <= n; i++)
    p[i] = 1;

  for (int i = 1; i <= m; i++) {
    int o = 0;
    memset(cnt, 0, sizeof cnt);
    for (int j = 1; j <= n; j++) {
      while (usd[a[j][p[j]]])
        ++p[j];
      int t = a[j][p[j]];
      if (++cnt[t] > cnt[o])
        o = t;
    }
    ans = min(ans, cnt[o]);
    usd[o] = 1;
  }
  printf("%d", ans);
  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int N, M;
int a[305][305];
int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      int c;
      cin >> c;
      c--;
      a[i][j] = c;
    }
  }
  vector<int> valid(M, 1);
  vector<vector<int>> head(M);
  vector<int> pos(M, 0);
  int ret = INT_MAX;
  for (int row = 0; row < N; ++row) {
    int h = a[row][0];
    head[h].push_back(row);
  }

  int cmax = 0;
  int ci = -1;
  for (int i = 0; i < M; ++i) {
    if (head[i].size() > cmax) {
      cmax = head[i].size();
      ci = i;
    }
  }

  ret = min(ret, cmax);
  int last = M;
  while (last > 1) {
    valid[ci] = 0;
    for (auto row : head[ci]) {
      int &ind = pos[row];
      while (valid[a[row][ind]] == 0)
        ind++;
      head[a[row][ind]].push_back(row);
    }

    head[ci].clear();
    int cmax = 0;
    for (int i = 0; i < M; ++i) {
      if (head[i].size() > cmax) {
        cmax = head[i].size();
        ci = i;
      }
    }

    ret = min(ret, cmax);
    last--;
  }

  cout << ret << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int N, M;
int a[305][305];
int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      int c;
      cin >> c;
      c--;
      a[i][j] = c;
    }
  }
  vector<int> valid(M, 1);
  vector<vector<int>> head(M);
  vector<int> pos(N, 0);
  int ret = INT_MAX;
  for (int row = 0; row < N; ++row) {
    int h = a[row][0];
    head[h].push_back(row);
  }

  int cmax = 0;
  int ci = -1;
  for (int i = 0; i < M; ++i) {
    if (head[i].size() > cmax) {
      cmax = head[i].size();
      ci = i;
    }
  }

  ret = min(ret, cmax);
  int last = M;
  while (last > 1) {
    valid[ci] = 0;
    for (auto row : head[ci]) {
      int &ind = pos[row];
      while (valid[a[row][ind]] == 0)
        ind++;
      head[a[row][ind]].push_back(row);
    }

    head[ci].clear();
    int cmax = 0;
    for (int i = 0; i < M; ++i) {
      if (head[i].size() > cmax) {
        cmax = head[i].size();
        ci = i;
      }
    }

    ret = min(ret, cmax);
    last--;
  }

  cout << ret << endl;
}
Problem p03652 (C++)

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

using namespace std;
typedef long long ll;

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> row(M, 0);
  vector<vector<int>> prefs(N, row);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cin >> prefs[i][j];
      prefs[i][j]--;
    }
  }

  int best = N;
  vector<int> inds(N, 0);
  vector<int> curs(M, 0);
  vector<bool> removed(M, false);

  for (int i = 0; i < N; i++) {
    curs[prefs[i][0]]++;
  }
  for (int k = 0; k < M; k++) {
    int curbestind = 0;
    for (int i = 0; i < M; i++) {
      if (curs[i] > curs[curbestind])
        curbestind = i;
    }

    if (curs[curbestind] < best)
      best = curs[curbestind];

    // remove sport curbestind
    removed[curbestind] = true;
    curs[curbestind] = 0;
    for (int i = 0; i < N; i++) {
      curs[prefs[i][inds[i]]]--;
      while (removed[prefs[i][inds[i]]]) {
        inds[i]++;
      }
      curs[prefs[i][inds[i]]]++;
    }
  }

  cout << best << endl;

  return 0;
}


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

using namespace std;
typedef long long ll;

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> row(M, 0);
  vector<vector<int>> prefs(N, row);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cin >> prefs[i][j];
      prefs[i][j]--;
    }
  }

  int best = N;
  vector<int> inds(N, 0);
  vector<int> curs(M, 0);
  vector<bool> removed(M, false);

  for (int i = 0; i < N; i++) {
    curs[prefs[i][0]]++;
  }
  for (int k = 0; k < M; k++) {
    int curbestind = 0;
    for (int i = 0; i < M; i++) {
      if (curs[i] > curs[curbestind])
        curbestind = i;
    }

    if (curs[curbestind] < best)
      best = curs[curbestind];

    // remove sport curbestind
    removed[curbestind] = true;
    curs[curbestind] = 0;
    for (int i = 0; i < N; i++) {
      curs[prefs[i][inds[i]]]--;
      while (removed[prefs[i][inds[i]]]) {
        inds[i]++;
      }
      if (inds[i] < M)
        curs[prefs[i][inds[i]]]++;
    }
  }

  cout << best << endl;

  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B, L, R;
string S, T;
const ll MOD = 998244353;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

int main() {
  cin >> N >> M;
  mat a(N, vec(M));
  rep(i, N) rep(j, M) cin >> a[i][j], --a[i][j];
  vector<bool> stop(N, false);
  vec id(N, 0), memo(M, 0);
  ll ans = N;
  rep(roop, M) {
    rep(i, N) {
      while (stop[a[i][id[i]]])
        ++id[i];
      ++memo[a[i][id[i]]];
    }
    auto ite = max_element(ALL(memo));
    ans = min(ans, *ite);
    stop[ite - memo.begin()] = true;
    rep(i, M) memo[i] = 0;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B, L, R;
string S, T;
const ll MOD = 998244353;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

int main() {
  cin >> N >> M;
  mat a(N, vec(M));
  rep(i, N) rep(j, M) cin >> a[i][j], --a[i][j];
  vector<bool> stop(M, false);
  vec id(N, 0), memo(M, 0);
  ll ans = N;
  rep(roop, M) {
    rep(i, N) {
      while (stop[a[i][id[i]]])
        ++id[i];
      ++memo[a[i][id[i]]];
    }
    auto ite = max_element(ALL(memo));
    ans = min(ans, *ite);
    stop[ite - memo.begin()] = true;
    rep(i, M) memo[i] = 0;
  }
  cout << ans << endl;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define pb push_back
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;
}

const int MOD = 1000000007;
const int INF = 1e9;
#define PI 3.14159265369;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  int n, m;
  cin >> n >> m;
  vector<queue<int>> q(n);
  rep(i, n) rep(j, m) {
    int a;
    cin >> a;
    q[i].push(--a);
  }
  int ans = INF;
  vector<bool> hold(n, true);
  rep(ri, m) {
    vector<int> cnt(m, 0);
    rep(i, n) {
      while (!hold[q[i].front()]) {
        q[i].pop();
      }
      cnt[q[i].front()]++;
    }
    int j = max_element(cnt.begin(), cnt.end()) - cnt.begin();
    hold[j] = false;
    chmin(ans, cnt[j]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define pb push_back
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;
}

const int MOD = 1000000007;
const int INF = 1e9;
#define PI 3.14159265369;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  int n, m;
  cin >> n >> m;
  vector<queue<int>> q(n);
  rep(i, n) rep(j, m) {
    int a;
    cin >> a;
    q[i].push(--a);
  }
  int ans = INF;
  vector<bool> hold(m, true);
  rep(ri, m) {
    vector<int> cnt(m, 0);
    rep(i, n) {
      while (!hold[q[i].front()]) {
        q[i].pop();
      }
      cnt[q[i].front()]++;
    }
    int j = max_element(cnt.begin(), cnt.end()) - cnt.begin();
    hold[j] = false;
    chmin(ans, cnt[j]);
  }
  cout << ans << endl;
}
Problem p03652 (Python)

Buggy submission (status: Runtime Error):
import os
import sys
from collections import Counter

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
A = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

A = np.array(A, dtype=int)
idx = np.zeros(N, dtype=int)
used = np.zeros(M + 1, dtype=bool)

ans = INF
for _ in range(N):
    k, cnt = Counter(A[np.arange(N), idx]).most_common(1)[0]
    ans = min(ans, cnt)
    used[k] = True
    for i in range(N):
        while idx[i] < M and used[A[i, idx[i]]]:
            idx[i] += 1
print(ans)


Fixed submission:
import os
import sys
from collections import Counter

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
A = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

A = np.array(A, dtype=int)
idx = np.zeros(N, dtype=int)
used = np.zeros(M + 1, dtype=bool)

ans = INF
for _ in range(M):
    k, cnt = Counter(A[np.arange(N), idx]).most_common(1)[0]
    ans = min(ans, cnt)
    used[k] = True
    for i in range(N):
        while idx[i] < M and used[A[i, idx[i]]]:
            idx[i] += 1
print(ans)
Problem p03652 (Python)

Buggy submission (status: Runtime Error):
import sys

readline = sys.stdin.readline
MOD = 10**9 + 7
INF = float("INF")
sys.setrecursionlimit(10**5)


def main():
    n, m = map(int, readline().split())
    a = []
    for i in range(n):
        a.append(list(map(int, readline().split())))

    ans = INF
    for _ in range(n - 1):
        cnt = [0] * (m + 1)
        for i in range(n):
            cnt[a[i][0]] += 1
        max_cnt = max(cnt)
        ans = min(ans, max_cnt)
        max_num = cnt.index(max_cnt)
        for i in range(n):
            a[i].remove(max_num)

    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys

readline = sys.stdin.readline
MOD = 10**9 + 7
INF = float("INF")
sys.setrecursionlimit(10**5)


def main():
    n, m = map(int, readline().split())
    a = []
    for i in range(n):
        a.append(list(map(int, readline().split())))

    ans = INF
    for _ in range(m):
        cnt = [0] * (m + 1)
        for i in range(n):
            cnt[a[i][0]] += 1
        max_cnt = max(cnt)
        ans = min(ans, max_cnt)
        max_num = cnt.index(max_cnt)
        for i in range(n):
            a[i].remove(max_num)

    print(ans)


if __name__ == "__main__":
    main()
Problem p03652 (C++)

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

using namespace std;

namespace {

#define VERA(i, a, b) for (auto i = (ll)(a); i < (ll)(b); ++i)
#define ANDY(i, a, b) for (auto i = (ll)(a); i > (ll)(b); --i)

typedef long long ll;
typedef long double dd;

const int MAX = 333;
int fav[MAX][MAX];
int at[MAX];

void Solve(ll test_num) {
  (void)test_num;
  int N, M;
  cin >> N >> M;
  VERA(i, 0, N) {
    VERA(j, 0, M) {
      cin >> fav[i][j];
      --fav[i][j];
    }
  }
  set<int> gone;
  int res = N;
  VERA(i, 0, N) at[i] = 0;
  VERA(m, 0, M) {
    vector<int> cnt(M, 0);
    VERA(i, 0, N)++ cnt[fav[i][at[i]]];
    const int hig_sport = max_element(cnt.begin(), cnt.end()) - cnt.begin();
    res = min(res, cnt[hig_sport]);
    gone.insert(hig_sport);
    VERA(i, 0, N) while (gone.count(fav[i][at[i]]))++ at[i];
  }
  cout << res << endl;
}

void Init() {}

} // namespace

int main() {
#ifdef AZN
  const auto start_time = chrono::system_clock::now();
  freopen("../input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  srand(1223);
  Init();
  ll tests = 1;
  //  cin >> tests;
  VERA(test, 1, tests + 1) { Solve(test); }
#ifdef AZN
  cerr << "Took: " << ((chrono::system_clock::now() - start_time).count() / 1e9)
       << " s" << endl;
#endif
  return 0;
}


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

using namespace std;

namespace {

#define VERA(i, a, b) for (auto i = (ll)(a); i < (ll)(b); ++i)
#define ANDY(i, a, b) for (auto i = (ll)(a); i > (ll)(b); --i)

typedef long long ll;
typedef long double dd;

const int MAX = 333;
int fav[MAX][MAX];
int at[MAX];

void Solve(ll test_num) {
  (void)test_num;
  int N, M;
  cin >> N >> M;
  VERA(i, 0, N) {
    VERA(j, 0, M) {
      cin >> fav[i][j];
      --fav[i][j];
    }
  }
  set<int> gone;
  int res = N;
  VERA(i, 0, N) at[i] = 0;
  VERA(m, 0, M) {
    vector<int> cnt(M, 0);
    VERA(i, 0, N)++ cnt[fav[i][at[i]]];
    const int hig_sport = max_element(cnt.begin(), cnt.end()) - cnt.begin();
    res = min(res, cnt[hig_sport]);
    gone.insert(hig_sport);
    VERA(i, 0, N) while (at[i] < M && gone.count(fav[i][at[i]]))++ at[i];
  }
  cout << res << endl;
}

void Init() {}

} // namespace

int main() {
#ifdef AZN
  const auto start_time = chrono::system_clock::now();
  freopen("../input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  srand(1223);
  Init();
  ll tests = 1;
  //  cin >> tests;
  VERA(test, 1, tests + 1) { Solve(test); }
#ifdef AZN
  cerr << "Took: " << ((chrono::system_clock::now() - start_time).count() / 1e9)
       << " s" << endl;
#endif
  return 0;
}
Problem p03652 (C++)

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

using ll = long long;

int N, M;
vector<vector<int>> A;
vector<int> s;

int solve() {
  vector<int> cnt(M);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      if (s[A[i][j]]) {
        cnt[A[i][j]]++;
        break;
      }
    }
  auto r = max_element(cnt.begin(), cnt.end());
  s[r - cnt.begin()]--;
  return *r;
}

int main() {
  cin >> N >> M;
  int res = N;
  A.resize(N, vector<int>(M));
  s.resize(N, 1);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      cin >> A[i][j], A[i][j]--;
  for (int i = 0; i < M - 1; i++) {
    res = min(res, solve());
  }
  cout << res << endl;
}

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

using ll = long long;

int N, M;
vector<vector<int>> A;
vector<int> s;

int solve() {
  vector<int> cnt(M);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      if (s[A[i][j]]) {
        cnt[A[i][j]]++;
        break;
      }
    }
  auto r = max_element(cnt.begin(), cnt.end());
  s[r - cnt.begin()]--;
  return *r;
}

int main() {
  cin >> N >> M;
  int res = N;
  A.resize(N, vector<int>(M));
  s.resize(M, 1);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      cin >> A[i][j], A[i][j]--;
  for (int i = 0; i < M - 1; i++) {
    res = min(res, solve());
  }
  cout << res << endl;
}
Problem p03652 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int n, m, i, j, st, dr, mid;
int a[305][305], viz[305], num[305], u[305];
int verif(int maxim) {
  int i, nr, ok, j;
  nr = m;
  for (i = 1; i <= m; i++) {
    viz[i] = 1;
    num[i] = 0;
  }
  for (i = 1; i <= n; i++) {
    num[a[i][1]]++;
    u[i] = 1;
  }
  do {
    ok = 0;
    for (i = 1; i <= m; i++) {
      if (num[i] > maxim) {
        ok = 1;
        viz[i] = 0;
        nr--;
      }
      num[i] = 0;
    }
    for (i = 1; i <= n; i++) {
      while (u[i] <= n && viz[a[i][u[i]]] == 0) {
        u[i]++;
      }
      num[a[i][u[i]]]++;
    }
  } while (ok == 1 && nr != 0);

  if (nr > 0) {
    return 1;
  }
  return 0;
}
int main() {
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  st = 1;
  dr = n;
  while (st <= dr) {
    mid = (st + dr) / 2;
    if (verif(mid)) {
      dr = mid - 1;
    } else {
      st = mid + 1;
    }
  }
  cout << st;
}


Fixed submission:
#include <iostream>
using namespace std;
int n, m, i, j, st, dr, mid;
int a[305][305], viz[305], num[305], u[305];
int verif(int maxim) {
  int i, nr, ok, j;
  nr = m;
  for (i = 1; i <= m; i++) {
    viz[i] = 1;
    num[i] = 0;
  }
  for (i = 1; i <= n; i++) {
    num[a[i][1]]++;
    u[i] = 1;
  }
  do {
    ok = 0;
    for (i = 1; i <= m; i++) {
      if (num[i] > maxim) {
        ok = 1;
        viz[i] = 0;
        nr--;
      }
      num[i] = 0;
    }
    for (i = 1; i <= n; i++) {
      while (u[i] <= m && viz[a[i][u[i]]] == 0) {
        u[i]++;
      }
      num[a[i][u[i]]]++;
    }
  } while (ok == 1 && nr != 0);

  if (nr > 0) {
    return 1;
  }
  return 0;
}
int main() {
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  st = 1;
  dr = n;
  while (st <= dr) {
    mid = (st + dr) / 2;
    if (verif(mid)) {
      dr = mid - 1;
    } else {
      st = mid + 1;
    }
  }
  cout << st;
}