Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <chrono>
#include <complex>
#include <condition_variable>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
// typedef long double ld;

typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vll> vvll;

constexpr ll MOD = 1e9 + 7;
// constexpr int INF = 1<<30;
// constexpr ll  LINF = 1LL<<60;

#define Sort(x) sort(x.begin(), x.end())
#define Reverse(x) reverse(x.begin(), x.end())
#define ABS(a, b) ((a) < (b) ? (b) - (a) : (a) - (b))
#define nbit(n) (1ll << (n))

// sort(begin(x), end(x), greater<ll>());
// lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置
// upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置

// Returns minimum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
  return a;
}

// Returns maximum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
  return a;
}

// a = (a+b)%MOD;
void addmod(ll &a, ll b) { a = (a + b) % MOD; }

template <typename T> void dumpContents(const T &v, const string &msg) {
  cerr << "### " << msg << " ###\n";
  for (const auto &x : v) {
    cerr << x << " ";
  }
  cerr << endl;
}

struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20) << fixed;
#define endl "\n"
  }
} before_main_function;

// ========== end of template ==========
int N;

int main(int argc, char **argv) {
  cin >> N;
  vll A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  multiset<ll> ret;
  ret.insert(A[0]);

  for (int i = 1; i < N; i++) {
    auto it = lower_bound(begin(ret), end(ret), A[i]);
    if (it != begin(ret)) {
      it--;
      ret.erase(it);
    }
    ret.insert(A[i]);
  }

  cout << ret.size() << endl;
  return 0;
}


Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <chrono>
#include <complex>
#include <condition_variable>
#include <deque>
#include <exception>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
// typedef long double ld;

typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vll> vvll;

constexpr ll MOD = 1e9 + 7;
// constexpr int INF = 1<<30;
// constexpr ll  LINF = 1LL<<60;

#define Sort(x) sort(x.begin(), x.end())
#define Reverse(x) reverse(x.begin(), x.end())
#define ABS(a, b) ((a) < (b) ? (b) - (a) : (a) - (b))
#define nbit(n) (1ll << (n))

// sort(begin(x), end(x), greater<ll>());
// lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置
// upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置

// Returns minimum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
  return a;
}

// Returns maximum of a and b.
// If a is less b, a is set to b.
template <typename T> T chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
  return a;
}

// a = (a+b)%MOD;
void addmod(ll &a, ll b) { a = (a + b) % MOD; }

template <typename T> void dumpContents(const T &v, const string &msg) {
  cerr << "### " << msg << " ###\n";
  for (const auto &x : v) {
    cerr << x << " ";
  }
  cerr << endl;
}

struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20) << fixed;
#define endl "\n"
  }
} before_main_function;

// ========== end of template ==========
int N;

int main(int argc, char **argv) {
  cin >> N;
  vll A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  multiset<ll> ret;
  ret.insert(A[0]);

  for (int i = 1; i < N; i++) {
    const ll a = A[i];
    auto it = ret.lower_bound(a);
    if (it != begin(ret)) {
      it--;
      ret.erase(it);
    }
    ret.insert(A[i]);
  }

  cout << ret.size() << endl;
  return 0;
}