Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#ifdef __LOCAL
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

#ifdef __LOCAL
#include <filesystem>
namespace fs = std::filesystem;
#endif

using namespace std;

using ll = long long int;
using pii = pair<int, int>;
inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}

int N, M;
vector<vector<int>> A;
void input() {
  fast_io();
#ifdef __LOCAL
  fs::path p = __FILE__;
  fs::path input, output;
  input = output = p.parent_path();
  input += string("/input/") + string(p.stem()) + string(".txt");
  output += string("/output/") + string(p.stem()) + string(".txt");
  freopen(input.c_str(), "r", stdin);
  freopen(output.c_str(), "w", stdout);
#endif
  cin >> N >> M;
  A.assign(N, vector<int>(M, 0));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      cin >> A[i][j];
}

void solve() {
  set<int> used;
  int ans = N + 1;
  while (used.size() < N) {
    unordered_map<int, int> cnt;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (used.find(A[i][j]) != used.end())
          continue;
        cnt[A[i][j]]++;
        break;
      }
    }
    auto itr = max_element(cnt.cbegin(), cnt.cend(),
                           [](pii a, pii b) { return a.second < b.second; });
    ans = min(ans, itr->second);
    used.insert(itr->first);
  }
  cout << ans << endl;
}

int main() {
  input();
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#ifdef __LOCAL
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

#ifdef __LOCAL
#include <filesystem>
namespace fs = std::filesystem;
#endif

using namespace std;

using ll = long long int;
using pii = pair<int, int>;
inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}

int N, M;
vector<vector<int>> A;
void input() {
  fast_io();
#ifdef __LOCAL
  fs::path p = __FILE__;
  fs::path input, output;
  input = output = p.parent_path();
  input += string("/input/") + string(p.stem()) + string(".txt");
  output += string("/output/") + string(p.stem()) + string(".txt");
  freopen(input.c_str(), "r", stdin);
  freopen(output.c_str(), "w", stdout);
#endif
  cin >> N >> M;
  A.assign(N, vector<int>(M, 0));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      cin >> A[i][j];
}

void solve() {
  set<int> used;
  int ans = N + 1;
  while (used.size() < M) {
    unordered_map<int, int> cnt;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (used.find(A[i][j]) != used.end())
          continue;
        cnt[A[i][j]]++;
        break;
      }
    }
    auto itr = max_element(cnt.cbegin(), cnt.cend(),
                           [](pii a, pii b) { return a.second < b.second; });
    ans = min(ans, itr->second);
    used.insert(itr->first);
  }
  cout << ans << endl;
}

int main() {
  input();
  solve();
  return 0;
}