Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define FASTIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define show(x) cerr << #x << " = " << x << "\n"
#define mem(a, b) memset(a, b, sizeof a)
#define popcnt(x) __builtin_popcount(x)
#define all(x) x.begin(), x.end()
#define mod(x, m) ((x % m + m) % m)
#define SZ(a) (int)a.size()
#define ll long long
#define ld long double
#define vii vector<ll>
#define dqi deque<ll>
#define sii set<ll>
#define vpi vector<pii>
#define mii map<ll, ll>
#define pii pair<ll, ll>
#define pi pair<int, int>
#define pb push_back
#define F first
#define S second
#define L length()
#define OO (ll)1e18
#define EPS 1e-9
using namespace std;
using namespace __gnu_pbds;
void ONLINE__JUDGE();

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
pii swapii(pii pr) { return {pr.S, pr.F}; }
ll binEXP(ll x, ll pw, ll MOD = LLONG_MAX);
int dx[] = {0, 0, -1, +1, -1, +1, -1, +1};
int dy[] = {-1, +1, 0, 0, -1, +1, +1, -1};

const ll MOD = 1e9 + 7;
const ll MAX = 1e2 + 9;

ll x;
multiset<ll> s;
vii vec;

int main() {
  // FASTIO ONLINE__JUDGE();

  cin >> x;
  vec.resize(x);
  for (ll &i : vec)
    cin >> i;

  s.insert(vec.front());
  for (int i = 1; i < x; ++i) {
    auto it = s.upper_bound(vec[i]);
    it--;

    if (*it < vec[i]) {
      s.erase(it);
      s.insert(vec[i]);
    } else {
      s.insert(vec[i]);
    }
  }
  cout << SZ(s);
  return 0;
}

void ONLINE__JUDGE() {
#ifndef ONLINE_JUDGE
  freopen("inp.c", "r", stdin);
// freopen( "OutPut.txt"   , "w" , stdout );
#endif // ONLINE_JUDGE
}

ll binEXP(ll x, ll pw, ll MOD) {
  ll res = 1;
  while (pw > 0) {
    pw % 2 ? res *= x : 0;
    x *= x;
    x %= MOD;
    pw /= 2;
    res %= MOD;
  }
  return res %= MOD;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define FASTIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define show(x) cerr << #x << " = " << x << "\n"
#define mem(a, b) memset(a, b, sizeof a)
#define popcnt(x) __builtin_popcount(x)
#define all(x) x.begin(), x.end()
#define mod(x, m) ((x % m + m) % m)
#define SZ(a) (int)a.size()
#define ll long long
#define ld long double
#define vii vector<ll>
#define dqi deque<ll>
#define sii set<ll>
#define vpi vector<pii>
#define mii map<ll, ll>
#define pii pair<ll, ll>
#define pi pair<int, int>
#define pb push_back
#define F first
#define S second
#define L length()
#define OO (ll)1e18
#define EPS 1e-9
using namespace std;
using namespace __gnu_pbds;
void ONLINE__JUDGE();

typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
pii swapii(pii pr) { return {pr.S, pr.F}; }
ll binEXP(ll x, ll pw, ll MOD = LLONG_MAX);
int dx[] = {0, 0, -1, +1, -1, +1, -1, +1};
int dy[] = {-1, +1, 0, 0, -1, +1, +1, -1};

const ll MOD = 1e9 + 7;
const ll MAX = 1e2 + 9;

ll x;
multiset<ll> s;
vii vec;

int main() {
  // FASTIO ONLINE__JUDGE();

  cin >> x;
  vec.resize(x);
  for (ll &i : vec)
    cin >> i;

  s.insert(vec.front());
  for (int i = 1; i < x; ++i) {
    auto it = s.lower_bound(vec[i]);

    if (it == s.begin()) {
      s.insert(vec[i]);
      continue;
    }
    it--;

    if (*it < vec[i]) {
      s.erase(it);
      s.insert(vec[i]);
    } else {
      s.insert(vec[i]);
    }
  }
  cout << SZ(s);
  return 0;
}

void ONLINE__JUDGE() {
#ifndef ONLINE_JUDGE
  freopen("inp.c", "r", stdin);
// freopen( "OutPut.txt"   , "w" , stdout );
#endif // ONLINE_JUDGE
}

ll binEXP(ll x, ll pw, ll MOD) {
  ll res = 1;
  while (pw > 0) {
    pw % 2 ? res *= x : 0;
    x *= x;
    x %= MOD;
    pw /= 2;
    res %= MOD;
  }
  return res %= MOD;
}