Problem p02642 (C++)

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

#define ll long long
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define all(x) (x).begin(), (x).end()
#define forn(i, a, b) for (int i = a; i < (signed)(b); ++i)
#define pb push_back

// debug templates
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N)                                                           \
  cerr << #A << " : [";                                                        \
  for (int i = 0; i < N; i++)                                                  \
    cerr << A[i] << " ";                                                       \
  cerr << "]\n";
#define debuga2(A, N, M)                                                       \
  cerr << #A << " : \n";                                                       \
  for (int i = 0; i < N; i++) {                                                \
    cerr << "[";                                                               \
    for (int j = 0; j < M; ++j)                                                \
      cerr << A[i][j] << " ";                                                  \
    cerr << "]\n";                                                             \
  }
#define debugp(p)                                                              \
  cerr << #p << " : "                                                          \
       << "(" << (p).first << "," << (p).second << ")\n";
#define debugv(v)                                                              \
  cerr << #v << " : "                                                          \
       << "[";                                                                 \
  for (int i = 0; i < (v).size(); i++)                                         \
    cerr << v[i] << " ";                                                       \
  cerr << "]\n";
#define debugv2(v)                                                             \
  cerr << #v << " : \n";                                                       \
  for (int i = 0; i < v.size(); i++) {                                         \
    cerr << "[";                                                               \
    for (int j = 0; j < (v[0].size()); ++j)                                    \
      cerr << v[i][j] << " ";                                                  \
    cerr << "]\n";                                                             \
  }
#define debugs(m)                                                              \
  cerr << #m << " : [ ";                                                       \
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \
    cerr << *itr << " ";                                                       \
  cerr << "]\n";
#define debugm(m)                                                              \
  cerr << #m << " : [ ";                                                       \
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \
    cerr << "(" << itr->first << "," << itr->second << ") ";                   \
  cerr << "]\n";

// 24 4 6 --> 2
// 24 4 6 2 --> 1
const int mxN = 1e6 + 2;
int mult[mxN];

void flag(int n) {
  for (int i = n; i < mxN; i += n) {
    mult[i] = 1;
  }
}

void solve() {
  int n;
  cin >> n;
  int a[n];
  forn(i, 0, n) cin >> a[i];
  sort(a, a + n);

  // catch duplicates
  forn(i, 1, n) {
    if (a[i] == a[i - 1])
      flag(a[i]);
  }
  int ans = 0;
  forn(i, 0, n) {
    int elm = a[i];
    if (mult[elm])
      continue;
    ++ans;
    flag(elm);
  }
  cout << ans;
}

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

  solve();
  return 0;
}


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

#define ll long long
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define all(x) (x).begin(), (x).end()
#define forn(i, a, b) for (int i = a; i < (signed)(b); ++i)
#define pb push_back

// debug templates
#define debug(x) cerr << #x << " : " << x << endl;
#define debuga(A, N)                                                           \
  cerr << #A << " : [";                                                        \
  for (int i = 0; i < N; i++)                                                  \
    cerr << A[i] << " ";                                                       \
  cerr << "]\n";
#define debuga2(A, N, M)                                                       \
  cerr << #A << " : \n";                                                       \
  for (int i = 0; i < N; i++) {                                                \
    cerr << "[";                                                               \
    for (int j = 0; j < M; ++j)                                                \
      cerr << A[i][j] << " ";                                                  \
    cerr << "]\n";                                                             \
  }
#define debugp(p)                                                              \
  cerr << #p << " : "                                                          \
       << "(" << (p).first << "," << (p).second << ")\n";
#define debugv(v)                                                              \
  cerr << #v << " : "                                                          \
       << "[";                                                                 \
  for (int i = 0; i < (v).size(); i++)                                         \
    cerr << v[i] << " ";                                                       \
  cerr << "]\n";
#define debugv2(v)                                                             \
  cerr << #v << " : \n";                                                       \
  for (int i = 0; i < v.size(); i++) {                                         \
    cerr << "[";                                                               \
    for (int j = 0; j < (v[0].size()); ++j)                                    \
      cerr << v[i][j] << " ";                                                  \
    cerr << "]\n";                                                             \
  }
#define debugs(m)                                                              \
  cerr << #m << " : [ ";                                                       \
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \
    cerr << *itr << " ";                                                       \
  cerr << "]\n";
#define debugm(m)                                                              \
  cerr << #m << " : [ ";                                                       \
  for (auto itr = m.begin(); itr != m.end(); itr++)                            \
    cerr << "(" << itr->first << "," << itr->second << ") ";                   \
  cerr << "]\n";

// 24 4 6 --> 2
// 24 4 6 2 --> 1
const int mxN = 1e6 + 2;
int mult[mxN];

void flag(int n) {
  for (int i = n; i < mxN; i += n) {
    mult[i] = 1;
  }
}

void solve() {
  int n;
  cin >> n;
  int a[n];
  forn(i, 0, n) cin >> a[i];
  sort(a, a + n);

  // catch duplicates
  forn(i, 1, n) {
    if (a[i] == a[i - 1] && !mult[a[i]])
      flag(a[i]);
  }
  int ans = 0;
  forn(i, 0, n) {
    int elm = a[i];
    if (mult[elm])
      continue;
    ++ans;
    flag(elm);
  }
  cout << ans;
}

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

  solve();
  return 0;
}