prithivMLmods/Coder-Stat huggingface dataset:
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}
int64_t rll() {
  long long n;
  scanf("%lld", &n);
  return n;
}

struct SegTree {
  std::vector<int> data;
  int n;
  SegTree(int n_) {
    for (n = 1; n < n_; n *= 2)
      ;
    data.resize(2 * n);
  }
  void set(int i, int val) {
    for (data[i += n] = val; i >>= 1;)
      data[i] = std::max(data[i << 1], data[i << 1 | 1]);
  }
  int max(int l, int r) {
    int res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (r & 1) {
        r--;
        res = std::max(res, data[r]);
      }
      if (l & 1) {
        res = std::max(res, data[l]);
        l++;
      }
    }
    return res;
  }
};

int main() {
  int n = ri();
  int a[n];
  for (int i = 0; i < n; i++)
    a[i] = ri();
  std::set<int> all;
  for (int i = 0; i < n; i++)
    all.insert(a[i]);
  std::map<int, int> comp;
  std::vector<int> decomp;
  int cnt = 0;
  for (auto i : all)
    comp[i] = cnt++, decomp.push_back(i);
  SegTree tree(all.size());
  for (int i = 0; i < n; i++) {
    tree.set(comp[a[i]], std::max(tree.data[tree.n + comp[a[i]]],
                                  tree.max(comp[a[i]], n) + 1));
  }
  std::cout << tree.max(0, tree.n) << std::endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}
int64_t rll() {
  long long n;
  scanf("%lld", &n);
  return n;
}

struct SegTree {
  std::vector<int> data;
  int n;
  SegTree(int n_) {
    for (n = 1; n < n_; n *= 2)
      ;
    data.resize(2 * n);
  }
  void set(int i, int val) {
    for (data[i += n] = val; i >>= 1;)
      data[i] = std::max(data[i << 1], data[i << 1 | 1]);
  }
  int max(int l, int r) {
    int res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (r & 1) {
        r--;
        res = std::max(res, data[r]);
      }
      if (l & 1) {
        res = std::max(res, data[l]);
        l++;
      }
    }
    return res;
  }
};

int main() {
  int n = ri();
  int a[n];
  for (int i = 0; i < n; i++)
    a[i] = ri();
  std::set<int> all;
  for (int i = 0; i < n; i++)
    all.insert(a[i]);
  std::map<int, int> comp;
  std::vector<int> decomp;
  int cnt = 0;
  for (auto i : all)
    comp[i] = cnt++, decomp.push_back(i);
  SegTree tree(all.size());
  for (int i = 0; i < n; i++) {
    tree.set(comp[a[i]], std::max(tree.data[tree.n + comp[a[i]]],
                                  tree.max(comp[a[i]], tree.n) + 1));
  }
  std::cout << tree.max(0, tree.n) << std::endl;

  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

void nibutan(vector<int> &dp, int le, int ri, int c) {
  if (ri - le == 1) {
    dp[ri] = c;
    return;
  }
  if (dp[(ri * le) / 2] < c) {
    nibutan(dp, le, (le + ri) / 2, c);
  } else {
    nibutan(dp, (le + ri) / 2, ri, c);
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> da(n);
  for (int i = 0; i < n; i++) {
    cin >> da[i];
  }
  vector<int> dp(1);
  dp[0] = da[0];
  int ans = 1;
  for (int i = 1; i < n; i++) {
    if (dp[ans - 1] >= da[i]) {
      dp.push_back(da[i]);
      ans++;
    } else {
      nibutan(dp, -1, ans - 1, da[i]);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

void nibutan(vector<int> &dp, int le, int ri, int c) {
  if (ri - le == 1) {
    dp[ri] = c;
    return;
  }
  if (dp[(ri + le) / 2] < c) {
    nibutan(dp, le, (le + ri) / 2, c);
  } else {
    nibutan(dp, (le + ri) / 2, ri, c);
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> da(n);
  for (int i = 0; i < n; i++) {
    cin >> da[i];
  }
  vector<int> dp(1);
  dp[0] = da[0];
  int ans = 1;
  for (int i = 1; i < n; i++) {
    if (dp[ans - 1] >= da[i]) {
      dp.push_back(da[i]);
      ans++;
    } else {
      nibutan(dp, -1, ans - 1, da[i]);
    }
  }
  cout << ans << endl;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  // priority_queue<pair<ll,int>> q;
  rep(i, n) { cin >> a[i]; }
  // sort(a.begin(),a.end());
  multiset<ll> p;
  p.insert(a[0]);

  for (int i = 1; i < n; i++) {
    // sort(p.begin(),p.end());
    auto c = lower_bound(p.begin(), p.end(), a[i]);
    // cout<< (c-p.begin())<<"c"<<endl;
    if (c == p.begin()) {
      p.insert(a[i]);
    } else {
      c--;
      p.erase(c);
      p.insert(a[i]);
    }
    //        rep(i,p.size())cout<<p[i];
    //        cout<<endl;
  }

  cout << p.size() << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  // priority_queue<pair<ll,int>> q;
  rep(i, n) { cin >> a[i]; }
  // sort(a.begin(),a.end());
  multiset<ll> p;
  p.insert(a[0]);

  for (int i = 1; i < n; i++) {
    // sort(p.begin(),p.end());
    auto c = p.lower_bound(a[i]);
    // cout<< (c-p.begin())<<"c"<<endl;
    if (c == p.begin()) {
      p.insert(a[i]);
    } else {
      c--;
      p.erase(c);
      p.insert(a[i]);
    }
    //        rep(i,p.size())cout<<p[i];
    //        cout<<endl;
  }

  cout << p.size() << endl;

  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < (int)N; i++)

int N;
vector<int> A;

int main() {
  cin >> N;
  A.resize(N);
  rep(i, N) cin >> A[i];

  multiset<int> v;

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

  cout << v.size() << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < (int)N; i++)

int N;
vector<int> A;

int main() {
  cin >> N;
  A.resize(N);
  rep(i, N) cin >> A[i];

  multiset<int> v;

  v.insert(A[0]);
  for (int i = 1; i < N; i++) {
    if (*v.begin() < A[i]) {
      auto it = v.lower_bound(A[i]);
      it--;
      v.erase(it);
    }
    v.insert(A[i]);
  }

  cout << v.size() << endl;

  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<vector<int>> v;
  multiset<int> s;
  vector<int> id(n);
  rep(i, n) {
    auto it = s.lower_bound(a[i]);
    if (it == s.begin()) {
      id[a[i]] = v.size();
      v.push_back(vector<int>{a[i]});
      s.insert(a[i]);
    } else {
      --it;
      id[a[i]] = id[*it];
      v[id[a[i]]].push_back(a[i]);
      s.erase(s.find(*it));
      s.insert(a[i]);
    }
  }

  int ans = v.size();
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<vector<int>> v;
  multiset<int> s;
  map<int, int> id;
  rep(i, n) {
    auto it = s.lower_bound(a[i]);
    if (it == s.begin()) {
      id[a[i]] = v.size();
      v.push_back(vector<int>{a[i]});
      s.insert(a[i]);
    } else {
      --it;
      id[a[i]] = id[*it];
      v[id[a[i]]].push_back(a[i]);
      s.erase(s.find(*it));
      s.insert(a[i]);
    }
  }

  int ans = v.size();
  cout << ans << endl;

  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using ll = long long;
using P = std::pair<ll, ll>;

#define rep(i, a, b) for (ll(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cout << i << "\n"

const int INF = 100000000;
const ll MOD = 1000000007;

ll pow_mod(ll x, ll y, ll mod) {
  if (y == 0)
    return 1;
  if (y % 2) {
    return (x * pow_mod(x, y - 1, mod)) % mod;
  } else {
    return pow_mod(x * x, y / 2, mod);
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  // 問題文中の添え字が0-indexか1-indexか確認!
  ll n;
  std::cin >> n;
  std::vector<ll> a(n), seq;
  rep(i, 0, n) { std::cin >> a[i]; }
  seq.push_back(a[0]);
  rep(i, 1, n) {
    if (a[i] <= seq[0]) {
      seq.push_back(a[i]);
      std::sort(all(seq));
    } else {
      auto itr = std::lower_bound(all(seq), a[i]);
      itr--;
      *itr = a[i];
    }
  }
  std::cout << seq.size();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using ll = long long;
using P = std::pair<ll, ll>;

#define rep(i, a, b) for (ll(i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(i) std::cout << i << "\n"

const int INF = 100000000;
const ll MOD = 1000000007;

ll pow_mod(ll x, ll y, ll mod) {
  if (y == 0)
    return 1;
  if (y % 2) {
    return (x * pow_mod(x, y - 1, mod)) % mod;
  } else {
    return pow_mod(x * x, y / 2, mod);
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  // 問題文中の添え字が0-indexか1-indexか確認!
  ll n;
  std::cin >> n;
  std::vector<ll> a(n), seq;
  rep(i, 0, n) { std::cin >> a[i]; }
  seq.push_back(a[0]);
  rep(i, 1, n) {
    if (a[i] <= seq[0]) {
      seq.insert(seq.begin(), a[i]);
    } else {
      auto itr = std::lower_bound(all(seq), a[i]);
      itr--;
      *itr = a[i];
    }
  }
  std::cout << seq.size();
  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
int a[200010];
multiset<int> s;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);
  s.insert(a[1]);
  for (int i = 2; i <= n; i++) {
    if (a[i] <= *s.begin())
      s.insert(a[i]);
    else {
      auto x = s.insert(a[i]);
      while (*x == a[i])
        x--;
      s.erase(x);
    }
  }
  printf("%d\n", s.size());
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
int a[200010];
multiset<int> s;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i);
  s.insert(a[1]);
  for (int i = 2; i <= n; i++) {
    if (a[i] <= *s.begin())
      s.insert(a[i]);
    else {
      s.insert(a[i]);
      auto x = s.lower_bound(a[i]);
      while (*x == a[i])
        x--;
      s.erase(x);
    }
  }
  printf("%d\n", s.size());
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

template <typename T>
typename std::vector<T>::iterator insert_sorted(std::vector<T> &vec,
                                                T const &item) {
  return vec.insert(std::upper_bound(vec.begin(), vec.end(), item), item);
}

int main() {
  int n;
  cin >> n;
  map<int, int> maxes;
  vector<int> roots;
  //  vector< vector<int>> g;

  for (int i = 0; i < n; i++) {
    int v;
    cin >> v;
    v = -1 * v;
    if (roots.size() <= 0) {
      roots.push_back(v);
      // maxes[v] = v;
      continue;
    }

    //    sort(roots.begin(), roots.end());
    auto index = upper_bound(roots.begin(), roots.end(), v);
    if (index != roots.end()) {
      roots.erase(index - 1);
    }
    insert_sorted(roots, v);

    // for(auto r : roots) {
    //   cout << r << " ";
    // }
    // cout << endl;

    // bool f = true;
    // for(int r : roots) {
    //   if( maxes[r] < v ) {
    //     maxes[r] = v;
    //     f = false;
    //     break;
    //   }
    // }

    // if(f){
    //   roots.push_back(v);
    //   maxes[v] = v;
    // }
  }
  cout << roots.size() << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

template <typename T>
typename std::vector<T>::iterator insert_sorted(std::vector<T> &vec,
                                                T const &item) {
  return vec.insert(std::upper_bound(vec.begin(), vec.end(), item), item);
}

int main() {
  int n;
  cin >> n;
  map<int, int> maxes;
  vector<int> roots;
  //  vector< vector<int>> g;

  for (int i = 0; i < n; i++) {
    int v;
    cin >> v;
    v = -1 * v;
    if (roots.size() <= 0) {
      roots.push_back(v);
      // maxes[v] = v;
      continue;
    }

    //    sort(roots.begin(), roots.end());
    auto index = upper_bound(roots.begin(), roots.end(), v);
    if (index != roots.end()) {
      roots.erase(index);
    }
    insert_sorted(roots, v);

    // for(auto r : roots) {
    //   cout << r << " ";
    // }
    // cout << endl;

    // bool f = true;
    // for(int r : roots) {
    //   if( maxes[r] < v ) {
    //     maxes[r] = v;
    //     f = false;
    //     break;
    //   }
    // }

    // if(f){
    //   roots.push_back(v);
    //   maxes[v] = v;
    // }
  }
  cout << roots.size() << endl;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  REP(i, n) cin >> v[i];
  vector<int> w;
  REP(i, n) {
    int ub = lower_bound(ALL(w), v[i]) - w.begin() - 1;
    if (ub < 0) {
      w.insert(w.begin(), v[i]);
    } else {
      w[ub] = v[i];
    }
    sort(ALL(w));
  }
  int ans = w.size();
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  REP(i, n) cin >> v[i];
  vector<int> w;
  REP(i, n) {
    int ub = lower_bound(ALL(w), v[i]) - w.begin() - 1;
    if (ub < 0) {
      w.insert(w.begin(), v[i]);
    } else {
      w[ub] = v[i];
    }
  }
  int ans = w.size();
  cout << ans << endl;
  return 0;
}
Problem p02973 (C++)

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

using namespace std;
typedef long long ll;
int main() {
  int N;
  cin >> N;

  multiset<ll> mp;

  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    auto itr = lower_bound(mp.begin(), mp.end(), a);
    if (itr == mp.begin()) {
      mp.insert(a);
    } else {
      itr--;
      mp.erase(itr);
      mp.insert(a);
    }
  }

  cout << (ll)mp.size() << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int main() {
  int N;
  cin >> N;

  multiset<ll> mp;

  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    auto itr = mp.lower_bound(a);
    if (itr == mp.begin()) {
      mp.insert(a);
    } else {
      itr--;
      mp.erase(itr);
      mp.insert(a);
    }
  }

  cout << (ll)mp.size() << endl;
  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int p = 1000000007;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define mmax(a, b) a = max(a, b)
#define mmin(a, b) a = min(a, b)
#define mkp make_pair
#define pin pair<int, int>
#define qin pair<pin, int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define sq(a) (a) * (a)
#define rev(s) reverse(s.begin(), s.end())
#define end_pr(s)                                                              \
  cout << s << endl;                                                           \
  return 0
#define itn int
#define bs bitset<5001>

int kai_size = 1000001;
vel kai(kai_size, 1);
vel ink(kai_size, 1);
vel dist;
signed main() {
  int n;
  cin >> n;
  V<pin> a(n);
  rep(i, n) {
    int x;
    cin >> x;
    a[i] = mkp(x, -i);
  }
  sor(a);
  vel b(n);
  rep(i, n) { b[-a[i].second] = i; }
  set<int> se;
  se.insert(-b[0]);
  for (int i = 1; i < n; i++) {
    auto itr = lower_bound(se.begin(), se.end(), -b[i]);
    if (itr == se.end()) {
      se.insert(-b[i]);
    } else {
      se.erase(itr);
      se.insert(-b[i]);
    }
  }
  cout << se.size() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int p = 1000000007;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define mmax(a, b) a = max(a, b)
#define mmin(a, b) a = min(a, b)
#define mkp make_pair
#define pin pair<int, int>
#define qin pair<pin, int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define sq(a) (a) * (a)
#define rev(s) reverse(s.begin(), s.end())
#define end_pr(s)                                                              \
  cout << s << endl;                                                           \
  return 0
#define itn int
#define bs bitset<5001>

int kai_size = 1000001;
vel kai(kai_size, 1);
vel ink(kai_size, 1);
vel dist;
signed main() {
  int n;
  cin >> n;
  V<pin> a(n);
  rep(i, n) {
    int x;
    cin >> x;
    a[i] = mkp(x, -i);
  }
  sor(a);
  vel b(n);
  rep(i, n) { b[-a[i].second] = i; }
  set<int> se;
  se.insert(-b[0]);
  for (int i = 1; i < n; i++) {
    auto itr = se.lower_bound(-b[i]);
    if (itr == se.end()) {
      se.insert(-b[i]);
    } else {
      se.erase(itr);
      se.insert(-b[i]);
    }
  }
  cout << se.size() << endl;
  return 0;
}
Problem p02973 (C++)

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

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)

using namespace std;

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

  ll N;
  cin >> N;
  vector<ll> A(N);
  Rep(i, N) { cin >> A[i]; }

  multiset<ll> color;
  Rep(i, N) {
    auto itr = color.upper_bound(-A[i]);
    // if (itr != color.end()) {
    //     color.erase(*itr);
    // }
    color.erase(itr);
    color.insert(-A[i]);
    // for (auto itr = color.begin(); itr != color.end(); itr++) {
    //     cout << -*itr << " ";
    // }
    // cout << "\n";
  }

  // for (auto itr = color.begin(); itr != color.end(); itr++) {
  //     cout << -*itr << " ";
  // }
  // cout << "\n";

  cout << color.size() << "\n";
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)

using namespace std;

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

  ll N;
  cin >> N;
  vector<ll> A(N);
  Rep(i, N) { cin >> A[i]; }

  multiset<ll> color;
  Rep(i, N) {
    auto itr = color.upper_bound(-A[i]);
    // cout << *itr << "\n";
    if (itr != color.end()) {
      color.erase(itr);
    }
    // color.erase(*itr);
    color.insert(-A[i]);
    // for (auto itr = color.begin(); itr != color.end(); itr++) {
    //     cout << -*itr << " ";
    // }
    // cout << "\n";
  }

  // for (auto itr = color.begin(); itr != color.end(); itr++) {
  //     cout << -*itr << " ";
  // }
  // cout << "\n";

  cout << color.size() << "\n";
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000001145
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
#define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
// typedef long long int ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};

int ans;
int n, m;
int a[111111];

signed main() {

  cin >> n;
  rep(i, n) cin >> a[i];
  multiset<int> s;
  rep(i, n) {
    auto itr = lower_bound(s.begin(), s.end(), a[i]);
    if (itr == s.begin())
      s.insert(a[i]);
    else {
      itr--;
      s.erase(itr);
      s.insert(a[i]);
    }
  }
  cout << s.size() << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000001145
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
#define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
// typedef long long int ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};

int ans;
int n, m;
int a[111111];

signed main() {

  cin >> n;
  rep(i, n) cin >> a[i];
  multiset<int> s;
  rep(i, n) {
    auto itr = s.lower_bound(a[i]);
    if (itr == s.begin())
      s.insert(a[i]);
    else {
      itr--;
      s.erase(itr);
      s.insert(a[i]);
    }
  }
  cout << s.size() << endl;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
#define INF 1LL << 60

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  multiset<int> color_tails;
  REP(i, N) {
    auto itr = upper_bound(color_tails.begin(), color_tails.end(), -A[i]);
    if (itr == color_tails.end()) {
      color_tails.insert(-A[i]);
    } else {
      color_tails.erase(itr);
      color_tails.insert(-A[i]);
    }
  }

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

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
#define INF 1LL << 60

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  multiset<int> color_tails;
  REP(i, N) {
    auto itr = color_tails.upper_bound(-A[i]);
    if (itr == color_tails.end()) {
      color_tails.insert(-A[i]);
    } else {
      color_tails.erase(itr);
      color_tails.insert(-A[i]);
    }
  }

  cout << color_tails.size() << endl;
  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define senky_bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
vector<ll> v[100005];
multiset<ll> st;
signed main() {
  senky_bansal IIIT_ALLAHABAD

      ll n;
  cin >> n;
  ll a[n];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int j = 0;
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      v[0].pb(a[i]);
      j++;
      st.insert(a[i]);
    } else {
      auto it1 = st.lower_bound(a[i]);
      if (it1 == st.begin()) {
        v[j].pb(a[i]);
        j++;
        st.insert(a[i]);
      } else {
        ll p = distance(st.begin(), it1);
        p--;
        v[p].pb(a[i]);
        it1--;
        st.erase(it1);
        st.insert(a[i]);
      }
    }
  }
  cout << j << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define senky_bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
vector<ll> v[100005];
multiset<ll> st;
signed main() {
  senky_bansal IIIT_ALLAHABAD

      ll n;
  cin >> n;
  ll a[n];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int j = 0;
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      v[0].pb(a[i]);
      j++;
      st.insert(a[i]);
    } else {
      auto it1 = st.lower_bound(a[i]);
      if (it1 == st.begin()) {
        v[j].pb(a[i]);
        j++;
        st.insert(a[i]);
      } else {
        it1--;
        st.erase(it1);
        st.insert(a[i]);
      }
    }
  }
  cout << j << endl;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll mod = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll A, ll B) {
  if (B == 0)
    return A;
  return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }
using Graph = vector<vector<int>>;
Graph G;

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

  int n;
  cin >> n;

  vector<int> b;
  rep(i, n) {
    int a;
    cin >> a;
    if (b.empty()) {
      b.push_back(a);
      continue;
    } else {
      auto itr = lower_bound(b.begin(), b.end(), a);
      int idx = distance(b.begin(), itr);
      // cout << idx << endl;
      if (idx == 0) {
        b.push_back(a);
        sort(b.begin(), b.end());
      } else
        b[idx - 1] = a;
    }
  }
  // for(auto it : b) cout << it << endl;
  cout << b.size() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define _rep(i, m, n) for (long long int i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll mod = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll A, ll B) {
  if (B == 0)
    return A;
  return gcd(B, A % B);
}
ll lcm(ll A, ll B) { return A * B / gcd(A, B); }
using Graph = vector<vector<int>>;
Graph G;

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

  int n;
  cin >> n;

  vector<int> b;
  rep(i, n) {
    int a;
    cin >> a;
    if (b.empty()) {
      b.push_back(a);
      continue;
    } else {
      auto itr = lower_bound(b.begin(), b.end(), a);
      int idx = distance(b.begin(), itr);
      if (idx == 0)
        b.insert(b.begin(), a);
      else
        b[idx - 1] = a;
    }
  }
  // for(auto it : b) cout << it << endl;
  cout << b.size() << endl;
}
Problem p02973 (Python)

Buggy submission (status: Runtime Error):
from bisect import bisect

n = int(input())
ma = [-int(input()) for _ in range(n)]

ans = [ma[0]]
for ai in ma[1:]:
    tmp = bisect(ans, ai)
    if tmp == len(ans):
        ans.append(ai)
    elif tmp < len(ans):
        ans[tmp] = ai
    print(ans)
print(len(ans))


Fixed submission:
from bisect import bisect

n = int(input())
ma = [-int(input()) for _ in range(n)]

ans = [ma[0]]
for ai in ma[1:]:
    tmp = bisect(ans, ai)
    if tmp == len(ans):
        ans.append(ai)
    elif tmp < len(ans):
        ans[tmp] = ai
print(len(ans))
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  vector<ll> results;
  rep(i, n) {
    auto it = lower_bound(results.begin(), results.end(), a[i]);

    if (it != results.begin())
      results.erase(it);
    results.insert(it, a[i]);
    continue;

    if (it == results.end()) {
      if (results.size() > 0 && a[i] > results[results.size() - 1])
        results[results.size() - 1] = a[i];
      else
        results.insert(results.begin(), a[i]);
    } else {
      if (it == results.begin()) {
        results.insert(results.begin(), a[i]);
      } else {
        *prev(it) = a[i];
      }
    }

    // cout<<results.size()<<':';rep(i, results.size())cout<<results[i]<<'
    // ';cout<<endl;
  }
  cout << results.size() << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  vector<ll> results;
  rep(i, n) {
    auto it = lower_bound(results.begin(), results.end(), a[i]);

    if (it != results.begin())
      *prev(it) = a[i];
    else
      results.insert(it, a[i]);

    // cout<<results.size()<<':';rep(i, results.size())cout<<results[i]<<'
    // ';cout<<endl;
  }
  cout << results.size() << endl;

  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;

int main() {
  int n;
  cin >> n;
  multiset<ll> ans;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll c = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (i == n - 1) {
      ans.insert(a[i]);
      i--;
    }
    auto it = upper_bound(ans.begin(), ans.end(), a[i]);
    if (it != ans.end())
      ans.erase(it);
    else
      c++;
    ans.insert(a[i]);
  }
  cout << c << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;

int main() {
  int n;
  cin >> n;
  multiset<ll> ans;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll c = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (i == n - 1) {
      ans.insert(a[i]);
      i--;
    }
    auto it = ans.upper_bound(a[i]);
    if (it != ans.end())
      ans.erase(it);
    else
      c++;
    ans.insert(a[i]);
  }
  cout << c << endl;

  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define endl '\n'
#define eb emplace_back
#define fst first
#define scd second

constexpr int mod = 1000000007;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

// value
int N;
int A[100010];

int backet[100];

void solve() {

  cin >> N;
  multiset<int> mm;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    auto it = lower_bound(mm.begin(), mm.end(), A[i]);
    if (it != mm.begin()) {
      mm.erase(--it);
    }
    mm.insert(A[i]);
  }

  cout << mm.size() << endl;

  return;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define endl '\n'
#define eb emplace_back
#define fst first
#define scd second

constexpr int mod = 1000000007;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

// value
int N;
int A[100010];

int backet[100];

void solve() {

  cin >> N;
  multiset<int> mm;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    auto it = mm.lower_bound(A[i]);
    if (it != mm.begin()) {
      mm.erase(--it);
    }
    mm.insert(A[i]);
  }

  cout << mm.size() << endl;

  return;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();

  return 0;
}
Problem p02973 (C++)

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

int main() {

  int n;
  scanf("%d", &n);

  multiset<int> mul;

  for (int i = 0; i < n; i++) {
    int temp;
    scanf("%d", &temp);

    auto indx = lower_bound(mul.begin(), mul.end(), temp);
    if (indx != mul.begin()) {
      indx--;
      mul.erase(indx);
      mul.insert(temp);
    } else
      mul.insert(temp);
  }
  cout << (int)mul.size() << endl;

  return 0;
}


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

int main() {

  int n;
  scanf("%d", &n);

  multiset<int> mul;

  for (int i = 0; i < n; i++) {
    int temp;
    scanf("%d", &temp);

    auto indx = mul.lower_bound(temp);
    if (indx != mul.begin()) {
      indx--;
      mul.erase(indx);
      mul.insert(temp);
    } else
      mul.insert(temp);
  }
  cout << (int)mul.size() << endl;

  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define ll long long
#define eps 1e-7
#define all(x) ((x).begin()), ((x).end())
#define usecppio                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
using pii = pair<int, int>;
#define MAXN 100000

vector<int> sequence;
int n, ldslen;
multiset<int> decrease;

void find_lds() {
  for (int i = 0; i < n; i++) {
    auto it = lower_bound(all(decrease), sequence[i]);
    if (it == decrease.begin())
      decrease.insert(sequence[i]);
    else {
      --it;
      decrease.erase(it);
      decrease.insert(sequence[i]);
    }
  }
  ldslen = decrease.size();
}

int main() {
  usecppio cin >> n;
  sequence.resize(n);
  for (int i = 0; i < n; i++)
    cin >> sequence[i];
  find_lds();
  cout << ldslen;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define ll long long
#define eps 1e-7
#define all(x) ((x).begin()), ((x).end())
#define usecppio                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
using pii = pair<int, int>;
#define MAXN 100000

vector<int> sequence;
int n, ldslen;
multiset<int> decrease;

void find_lds() {
  for (int i = 0; i < n; i++) {
    auto it = decrease.lower_bound(sequence[i]);
    if (it == decrease.begin())
      decrease.insert(sequence[i]);
    else {
      --it;
      decrease.erase(it);
      decrease.insert(sequence[i]);
    }
  }
  ldslen = decrease.size();
}

int main() {
  usecppio cin >> n;
  sequence.resize(n);
  for (int i = 0; i < n; i++)
    cin >> sequence[i];
  find_lds();
  cout << ldslen;
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define N 100000
typedef long long ll;
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) int(x.size())
#define OUT(a) cout << (a) << endl
#define VECIN(type, c, n)                                                      \
  vector<type> c(n);                                                           \
  for (auto &i : c)                                                            \
    cin >> i;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  REP(i, n) { cin >> a[i]; }
  vector<int> b;
  b.push_back(a[n - 1]);
  for (int i = n - 2; i >= 0; i--) {
    if (a[i] >= b[SZ(b) - 1]) {
      b.push_back(a[i]);
    } else {
      auto x = upper_bound(ALL(b), a[i]);
      b[b.begin() - x] = a[i];
    }
  }
  OUT(SZ(b));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define N 100000
typedef long long ll;
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) int(x.size())
#define OUT(a) cout << (a) << endl
#define VECIN(type, c, n)                                                      \
  vector<type> c(n);                                                           \
  for (auto &i : c)                                                            \
    cin >> i;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  REP(i, n) { cin >> a[i]; }
  vector<int> b;
  b.push_back(a[n - 1]);
  for (int i = n - 2; i >= 0; i--) {
    if (a[i] >= b[SZ(b) - 1]) {
      b.push_back(a[i]);
    } else {
      auto x = upper_bound(ALL(b), a[i]);
      b[x - b.begin()] = a[i];
    }
  }
  OUT(SZ(b));
  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<char> vc;

int main() {
  int N;
  cin >> N;
  /*vl A(N);
  REP(i, N){
      cin >> A[i];
  }*/
  vl max;
  int A;
  cin >> A;
  max.push_back(-A);
  FOR(i, 1, N) {
    int tmp;
    cin >> tmp;
    bool isIn = false;
    int num = max.size();
    REP(j, num) {
      if (-max[j] < tmp) {
        max[j] = -tmp;
        isIn = true;
        break;
      }
    }
    if (!isIn)
      max.push_back(-tmp);
  }
  cout << max.size() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<char> vc;

int main() {
  int N;
  cin >> N;
  /*vl A(N);
  REP(i, N){
      cin >> A[i];
  }*/
  vl max;
  int A;
  cin >> A;
  max.push_back(-A);
  FOR(i, 1, N) {
    int tmp;
    cin >> tmp;
    bool isIn = false;
    int num = max.size();
    if (-max[num - 1] >= tmp) {
      max.push_back(-tmp);
      continue;
    }
    REP(j, num) {
      if (-max[j] < tmp) {
        max[j] = -tmp;
        isIn = true;
        break;
      }
    }
    if (!isIn)
      max.push_back(-tmp);
  }
  cout << max.size() << endl;
  return 0;
}
Problem p02973 (C++)

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

#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x.size())
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define uniq(x) x.erase(unique(x.begin(), x.end()), x.end())
#define F first
#define S second
#define mp make_pair
#define eb emplace_back
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

typedef long long ll;

/* --- INFの値が適切か確認する --- */
// const int INF = 1 << 30;
// const ll INF = 1LL << 60;
// const int MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  multiset<int> ms;
  rep(i, n) {
    auto it = lower_bound(all(ms), a[i]);
    if (it != ms.begin())
      ms.erase(--it);
    ms.insert(a[i]);
  }

  cout << sz(ms) << endl;
}


Fixed submission:
#include <bits/stdc++.h>

#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x.size())
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define uniq(x) x.erase(unique(x.begin(), x.end()), x.end())
#define F first
#define S second
#define mp make_pair
#define eb emplace_back
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

typedef long long ll;

/* --- INFの値が適切か確認する --- */
// const int INF = 1 << 30;
// const ll INF = 1LL << 60;
// const int MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  multiset<int> ms;
  rep(i, n) {
    auto it = ms.lower_bound(a[i]);
    if (it != ms.begin())
      ms.erase(--it);
    ms.insert(a[i]);
  }

  cout << sz(ms) << endl;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pi 3.1415926536
#define pb push_back
#define N 1000000007
#define M 998244353
#define INF 1e9
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, i, j, k, t, l;
  cin >> n;
  int a[n + 5];
  multiset<ll> m;
  multiset<ll>::iterator it;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    it = lower_bound(m.begin(), m.end(), a[i]);
    if (it == m.begin()) {
      m.insert(a[i]);
    } else {
      it--;
      m.erase(it);
      m.insert(a[i]);
    }
  }
  cout << m.size() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pi 3.1415926536
#define pb push_back
#define N 1000000007
#define M 998244353
#define INF 1e9
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, i, j, k, t, l;
  cin >> n;
  int a[n + 5];
  multiset<ll> m;
  multiset<ll>::iterator it;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    it = m.lower_bound(a[i]);
    if (it == m.begin()) {
      m.insert(a[i]);
    } else {
      it--;
      m.erase(it);
      m.insert(a[i]);
    }
  }
  cout << m.size() << endl;
}