Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;

#define in(v)                                                                  \
  v;                                                                           \
  cin >> v;
void ins() {}
template <class T, class... Rest> void ins(T &v, Rest &...rest) {
  cin >> v;
  ins(rest...);
}

#define rep(i, n) for (int i = 0, _i = (n); i < _i; ++i)
#define rrep(i, n) for (long long i = (n); i >= 0; --i)
#define all(f, c, ...)                                                         \
  (([&](decltype((c)) cccc) {                                                  \
    return (f)(begin(cccc), end(cccc), ##__VA_ARGS__);                         \
  })(c))

// ========== debug ==========
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (size_t i = 0; i < vec.size(); ++i)
    os << (i ? ", " : "") << vec[i];
  os << "}";
  return os;
}
ostream &operator<<(ostream &os, const vector<char> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    os << v[i];
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}

#ifdef LOCAL
void debug() { cerr << "\n"; }
template <class First> void debug(const First &first) { cerr << first << "\n"; }
template <class First, class... Rest>
void debug(const First &first, const Rest &...rest) {
  cerr << first << ",";
  debug(rest...);
}
void debug2() { cerr << "\n"; }
template <class First> void debug2(const First &first) { cerr << first << " "; }
template <class First, class... Rest>
void debug2(const First &first, const Rest &...rest) {
  cerr << first << " ";
  debug2(rest...);
}
#else
#define debug(...) 42
#define debug2(...) 42
#endif
// ===========================

int main() {
  int N;
  ins(N);

  vi A(N);
  rep(i, N) { cin >> A[i]; }
  all(sort, A);

  vector<bool> ok(1e6 + 1, true);

  int ans = 0;
  rep(i, N) {
    if (all(upper_bound, A, A[i]) - all(lower_bound, A, A[i]) > 1) {
      ok[A[i]] = false;
      for (int x = 2 * A[i]; x <= 1e6; x += A[i]) {
        ok[x] = false;
      }
      continue;
    }
    if (!ok[A[i]])
      continue;
    ++ans;
    for (int x = 2 * A[i]; x <= 1e6; x += A[i]) {
      ok[x] = false;
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;

#define in(v)                                                                  \
  v;                                                                           \
  cin >> v;
void ins() {}
template <class T, class... Rest> void ins(T &v, Rest &...rest) {
  cin >> v;
  ins(rest...);
}

#define rep(i, n) for (int i = 0, _i = (n); i < _i; ++i)
#define rrep(i, n) for (long long i = (n); i >= 0; --i)
#define all(f, c, ...)                                                         \
  (([&](decltype((c)) cccc) {                                                  \
    return (f)(begin(cccc), end(cccc), ##__VA_ARGS__);                         \
  })(c))

// ========== debug ==========
template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (size_t i = 0; i < vec.size(); ++i)
    os << (i ? ", " : "") << vec[i];
  os << "}";
  return os;
}
ostream &operator<<(ostream &os, const vector<char> &v) {
  for (size_t i = 0; i < v.size(); ++i)
    os << v[i];
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}

#ifdef LOCAL
void debug() { cerr << "\n"; }
template <class First> void debug(const First &first) { cerr << first << "\n"; }
template <class First, class... Rest>
void debug(const First &first, const Rest &...rest) {
  cerr << first << ",";
  debug(rest...);
}
void debug2() { cerr << "\n"; }
template <class First> void debug2(const First &first) { cerr << first << " "; }
template <class First, class... Rest>
void debug2(const First &first, const Rest &...rest) {
  cerr << first << " ";
  debug2(rest...);
}
#else
#define debug(...) 42
#define debug2(...) 42
#endif
// ===========================

int main() {
  int N;
  ins(N);

  vi A(N);
  rep(i, N) { cin >> A[i]; }
  all(sort, A);

  vector<bool> ok(1e6 + 1, true);

  int ans = 0;
  rep(i, N) {
    if (all(upper_bound, A, A[i]) - all(lower_bound, A, A[i]) > 1) {
      if (ok[A[i]]) {
        ok[A[i]] = false;
        for (int x = 2 * A[i]; x <= 1e6; x += A[i]) {
          ok[x] = false;
        }
      }
      continue;
    }
    if (!ok[A[i]])
      continue;
    ++ans;
    for (int x = 2 * A[i]; x <= 1e6; x += A[i]) {
      ok[x] = false;
    }
  }
  cout << ans << endl;

  return 0;
}