Problem p03487 (C++)

Buggy submission (status: Time Limit Exceeded):
// SeeAlso:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define MAX 100000
#define NIL -1
#define MOD 1000000007

typedef int _loop_int;
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define debug(x) cout << #x << ": " << x << endl
#define debig_vec(v)                                                           \
  cout << #v << ":";                                                           \
  rep(i, v.size()) cout << " " << v[i];                                        \
  cout << endl
#define ALL(a) (a).begin(), (a).end()

// 最大公約数
inline constexpr ll gcd(ll a, ll b) {
  if (!a || !b)
    return 0;
  while (b) {
    ll c = b;
    b = a % b;
    a = c;
  }
  return a;
}
// 最小公倍数
inline constexpr ll lcm(ll a, ll b) {
  if (!a || !b)
    return 0;
  return a * b / gcd(a, b);
}

#define print2D(h, w, arr)                                                     \
  rep(i, h) {                                                                  \
    rep(j, w) cout << arr[i][j] << " ";                                        \
    cout << endl;                                                              \
  }
template <class T> void print(const T &x) {
  cout << setprecision(12) << x << endl;
}
template <class T, class... A> void print(const T &first, const A &...rest) {
  cout << first << " ";
  print(rest...);
}

int main() {
  ll n;
  map<ll, ll> a;
  cin >> n;
  rep(i, n) {
    ll b;
    cin >> b;
    bool flag = true;
    for (auto it = a.begin(); it != a.end(); it++) {
      auto p = *it;
      if (p.first == b) {
        a.erase(p.first);
        a.insert(make_pair(p.first, p.second + 1));
        flag = false;
        break;
      }
    }

    if (flag) {
      a.insert(make_pair(b, 1));
    }
  }
  ll ans = 0;

  for (auto it = a.begin(); it != a.end(); it++) {
    auto p = *it;
    if (p.first > p.second) {
      ans += p.second;
    } else {
      ans += p.second - p.first;
    }
  }

  print(ans);
  return 0;
}


Fixed submission:
// SeeAlso:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define MAX 100000
#define NIL -1
#define MOD 1000000007

typedef int _loop_int;
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define debug(x) cout << #x << ": " << x << endl
#define debig_vec(v)                                                           \
  cout << #v << ":";                                                           \
  rep(i, v.size()) cout << " " << v[i];                                        \
  cout << endl
#define ALL(a) (a).begin(), (a).end()

// 最大公約数
inline constexpr ll gcd(ll a, ll b) {
  if (!a || !b)
    return 0;
  while (b) {
    ll c = b;
    b = a % b;
    a = c;
  }
  return a;
}
// 最小公倍数
inline constexpr ll lcm(ll a, ll b) {
  if (!a || !b)
    return 0;
  return a * b / gcd(a, b);
}

#define print2D(h, w, arr)                                                     \
  rep(i, h) {                                                                  \
    rep(j, w) cout << arr[i][j] << " ";                                        \
    cout << endl;                                                              \
  }
template <class T> void print(const T &x) {
  cout << setprecision(12) << x << endl;
}
template <class T, class... A> void print(const T &first, const A &...rest) {
  cout << first << " ";
  print(rest...);
}

int main() {
  ll n;
  map<ll, ll> a;
  cin >> n;
  rep(i, n) {
    ll b;
    cin >> b;
    try {
      auto temp = a.at(b);
      a.erase(b);
      a.insert(make_pair(b, temp + 1));
    } catch (std::out_of_range) {
      a.insert(make_pair(b, 1));
    }
  }
  ll ans = 0;

  for (auto it = a.begin(); it != a.end(); it++) {
    auto p = *it;
    if (p.first > p.second) {
      ans += p.second;
    } else {
      ans += p.second - p.first;
    }
  }

  print(ans);
  return 0;
}