Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define YYS(x, arr) for (auto &x : arr)
#define PW(x) (1LL << (x))
#define SZ(x) ((ll)(x).size())

#define pb emplace_back
#define fi first
#define se second
using ll = long long;
using P = pair<ll, ll>;
// using Tu = tuple<ll, ll, ll>;
const ll INF = 1LL << 61LL;
const ll MOD = 1e9 + 7;

using ld = long double;

template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class T> void UNIQUE(vector<T> &a) {
  a.erase(unique(a.begin(), a.end()), a.end());
}

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

ll in() {
  long long int x;
  assert(scanf("%lld", &x) == 1);
  return x;
}
ld fin() {
  double x;
  assert(scanf("%lf", &x) == 1);
  return x;
}
char yuyushiki[1000010];
string stin() {
  assert(scanf("%s", yuyushiki) == 1);
  return yuyushiki;
}

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> x(N, vector<ll>(M));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      cin >> x[i][j];
      x[i][j]--;
    }
  vector<bool> alive(N, true);
  vector<ll> now(N);
  vector<ll> player(M);
  ll ans = INF;
  for (int i = 0; i < N; i++)
    player[x[i][0]]++;
  for (int i = 0; i < M; i++) {
    // for(auto &&x : player)
    //   cout << x << " ";
    // cout << endl;
    ll ma = -1;
    ll maarg = -1;
    for (int j = 0; j < M; j++) {
      if (player[j] > ma) {
        ma = player[j];
        maarg = j;
      }
      player[j] = 0;
    }
    alive[maarg] = false;
    // cout << ans << " " << ma << endl;
    ans = min(ans, ma);
    if (i == M - 1)
      break;
    for (int j = 0; j < N; j++) {
      while (!alive[x[j][now[j]]])
        now[j]++;
      player[x[j][now[j]]]++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define YYS(x, arr) for (auto &x : arr)
#define PW(x) (1LL << (x))
#define SZ(x) ((ll)(x).size())

#define pb emplace_back
#define fi first
#define se second
using ll = long long;
using P = pair<ll, ll>;
// using Tu = tuple<ll, ll, ll>;
const ll INF = 1LL << 61LL;
const ll MOD = 1e9 + 7;

using ld = long double;

template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class T> void UNIQUE(vector<T> &a) {
  a.erase(unique(a.begin(), a.end()), a.end());
}

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

ll in() {
  long long int x;
  assert(scanf("%lld", &x) == 1);
  return x;
}
ld fin() {
  double x;
  assert(scanf("%lf", &x) == 1);
  return x;
}
char yuyushiki[1000010];
string stin() {
  assert(scanf("%s", yuyushiki) == 1);
  return yuyushiki;
}

int main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<ll>> x(N, vector<ll>(M));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      cin >> x[i][j];
      x[i][j]--;
    }
  vector<bool> alive(M, true);
  vector<ll> now(N);
  vector<ll> player(M);
  ll ans = INF;
  for (int i = 0; i < N; i++)
    player[x[i][0]]++;
  for (int i = 0; i < M; i++) {
    // for(auto &&x : player)
    //   cout << x << " ";
    // cout << endl;
    ll ma = -1;
    ll maarg = -1;
    for (int j = 0; j < M; j++) {
      if (player[j] > ma) {
        ma = player[j];
        maarg = j;
      }
      player[j] = 0;
    }
    alive[maarg] = false;
    // cout << ans << " " << ma << endl;
    ans = min(ans, ma);
    if (i == M - 1)
      break;
    for (int j = 0; j < N; j++) {
      while (!alive[x[j][now[j]]])
        now[j]++;
      player[x[j][now[j]]]++;
    }
  }
  cout << ans << endl;
}