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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  reverse(a.begin(), a.end());
  int ans = 1;
  vector<int> p;
  p.push_back(a[0]);
  for (int i = 1; i < n; i++) {
    sort(p.begin(), p.end());
    int x = upper_bound(p.begin(), p.end(), a[i]) - p.begin();
    if (x >= p.size()) {
      p.push_back(a[i]);
      ans++;
    } else {
      p[x] = a[i];
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  reverse(a.begin(), a.end());
  int ans = 1;
  vector<int> p;
  p.push_back(a[0]);
  for (int i = 1; i < n; i++) {
    int x = upper_bound(p.begin(), p.end(), a[i]) - p.begin();
    if (x >= p.size()) {
      p.push_back(a[i]);
      ans++;
    } else {
      p[x] = a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define fs first
#define sc second
#define INF 1000000000
#define MOD 1000000007
#define EPS 0.00000001

int main() {
  int N;
  cin >> N;
  multiset<int> S;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    auto it = lower_bound(S.begin(), S.end(), a);
    if (it != S.begin()) {
      it--;
      S.erase(it);
    }
    S.insert(a);
  }
  cout << S.size() << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define fs first
#define sc second
#define INF 1000000000
#define MOD 1000000007
#define EPS 0.00000001

int main() {
  int N;
  cin >> N;
  multiset<int> S;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    auto it = S.lower_bound(a);
    if (it != S.begin())
      S.erase(--it);
    S.insert(a);
  }
  cout << S.size() << endl;
  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define loop(n, i, a) for (int i = a; i < n; i++)
#define all(arr, n) arr, arr + n
#define allv(v) (v).begin(), (v).end()
#define rallv(v) (v).rbegin(), (v).rend()
#define m_p make_pair
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define sz(x) (int)x.size()
#define pb push_back
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
  int n;
  cin >> n;
  multiset<int> st;
  loop(n, i, 0) {
    int x;
    cin >> x;
    if (i == 0)
      st.insert(x);
    else {
      auto it = st.lower_bound(x);
      it--;
      if (x > *it) {
        st.erase(it);
        st.insert(x);
      } else {
        st.insert(x);
      }
    }
  }
  cout << sz(st) << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define loop(n, i, a) for (int i = a; i < n; i++)
#define all(arr, n) arr, arr + n
#define allv(v) (v).begin(), (v).end()
#define rallv(v) (v).rbegin(), (v).rend()
#define m_p make_pair
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define sz(x) (int)x.size()
#define pb push_back
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main() {
  int n;
  cin >> n;
  multiset<int> st;
  loop(n, i, 0) {
    int x;
    cin >> x;
    if (i == 0)
      st.insert(x);
    else {
      auto it = st.lower_bound(x);
      if (it != st.begin())
        st.erase(--it);
      st.insert(x);
    }
  }
  cout << sz(st) << endl;
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  multiset<ll> s;
  rep(i, 0, n) {
    ll a;
    cin >> a;
    auto ite = s.lower_bound(a);
    if (ite != s.begin())
      s.erase(ite--);
    s.insert(a);
  }
  cout << s.size() << endl;
}


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

int main() {
  ll n;
  cin >> n;
  multiset<ll> s;
  rep(i, 0, n) {
    ll a;
    cin >> a;
    auto ite = s.lower_bound(a);
    if (ite != s.begin()) {
      ite--;
      s.erase(ite);
    }
    s.insert(a);
  }
  cout << s.size() << endl;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  int N;
  cin >> N;

  vector<ll> A(N, 0);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<int> min_color(N, 1);
  vector<ll> color_min_val;
  color_min_val.push_back(A[0]);
  for (int i = 1; i < N; i++) {
    auto Iter = lower_bound(color_min_val.begin(), color_min_val.end(), A[i]);
    // int dist = distance(color_min_val.begin(), Iter);
    if (Iter == color_min_val.begin()) { //) {
      color_min_val.push_back(A[i]);
      sort(color_min_val.begin(), color_min_val.end());
      min_color[i] = min_color[i - 1] + 1;
    } else {
      *(Iter - 1) = A[i];
      min_color[i] = min_color[i - 1];
    }
  }

  cout << min_color[N - 1] << endl;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  int N;
  cin >> N;

  vector<ll> A(N, 0);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<int> min_color(N, 1);
  vector<ll> color_min_val;
  color_min_val.push_back(A[0]);
  for (int i = 1; i < N; i++) {
    auto Iter = lower_bound(color_min_val.begin(), color_min_val.end(), A[i]);
    // int dist = distance(color_min_val.begin(), Iter);
    if (Iter == color_min_val.begin()) { //) {
      color_min_val.insert(color_min_val.begin(), A[i]);
      min_color[i] = min_color[i - 1] + 1;
    } else {
      *(Iter - 1) = A[i];
      min_color[i] = min_color[i - 1];
    }
  }

  cout << min_color[N - 1] << endl;
}
Problem p02973 (C++)

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

// 型定義
typedef long long ll;

// 定数
const int INF = 1e+9;
const int MOD = 1e+9 + 7;

// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)

// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size())      // size
#define MAX(x) *max_element(ALL(x))   // 最大値
#define MIN(x) *min_element(ALL(x))   // 最小値

// グローバル変数

// ライブラリ挿入

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  multiset<int> st;
  st.insert(A[0]);
  REP2(i, 1, N) {
    auto itr = lower_bound(st.begin(), st.end(), A[i]);
    if (itr == st.begin()) {
      st.insert(A[i]);
    } else {
      itr--;
      st.erase(itr);
      st.insert(A[i]);
    }
  }

  cout << st.size() << endl;
}

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

// 型定義
typedef long long ll;

// 定数
const int INF = 1e+9;
const int MOD = 1e+9 + 7;

// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)

// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size())      // size
#define MAX(x) *max_element(ALL(x))   // 最大値
#define MIN(x) *min_element(ALL(x))   // 最小値

// グローバル変数

// ライブラリ挿入

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  multiset<int> st;
  st.insert(A[0]);
  REP2(i, 1, N) {
    auto itr = st.lower_bound(A[i]);
    if (itr == st.begin()) {
      st.insert(A[i]);
    } else {
      itr--;
      st.erase(itr);
      st.insert(A[i]);
    }
  }

  cout << st.size() << endl;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define pp pair<int, int>
#define ll long long
#define ld long double
int inf = 100000000;
ll INF = 4000000000000000000;
ll MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(0);
  int ans = 1;
  rep(i, n) cin >> a.at(i);
  reverse(a.begin(), a.end());
  b.push_back(a.at(0));
  int ma = a.at(0);
  for (int i = 1; i < n; i++) {
    int k = a.at(i);
    // cout << k << ma << endl;
    if (k >= ma) {
      ans++;
      ma = k;
      b.push_back(k);
      continue;
    }
    auto it = upper_bound(b.begin(), b.end(), k);

    b.at(it - b.begin()) = k;
    sort(b.begin(), b.end());
    if (it - b.begin() == b.size() - 1) {
      ma = b.at(b.size() - 1);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define pp pair<int, int>
#define ll long long
#define ld long double
int inf = 100000000;
ll INF = 4000000000000000000;
ll MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(0);
  int ans = 1;
  rep(i, n) cin >> a.at(i);
  reverse(a.begin(), a.end());
  b.push_back(a.at(0));
  int ma = a.at(0);
  for (int i = 1; i < n; i++) {
    int k = a.at(i);
    // cout << k << ma << endl;
    if (k >= ma) {
      ans++;
      ma = k;
      b.push_back(k);
      continue;
    }
    auto it = upper_bound(b.begin(), b.end(), k);

    b.at(it - b.begin()) = k;
    if (it - b.begin() == b.size() - 1) {
      ma = b.at(b.size() - 1);
    }
  }
  cout << ans << endl;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

typedef unsigned long long int ulint;
typedef long long int lint;

int main() {
  ulint N;
  cin >> N;

  vector<ulint> A;

  for (int i = 0; i < N; ++i) {
    ulint a;
    cin >> a;
    A.push_back(a);
  }

  vector<ulint> C(N, (ulint)-1);

  for (int i = N - 1; i >= 0; --i) {
    int j = 0;
    while (A[i] >= C[j]) {
      ++j;
    }
    C[j] = A[i];
  }

  // cout << "C= " << endl;
  // for (int i = 0; i < N; ++i) {
  //   cout << C[i] << " ";
  // }
  // cout << endl;

  auto it = find(C.begin(), C.end(), (ulint)-1);

  ulint val = it - C.begin();

  cout << val << endl;
}


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

typedef unsigned long long int ulint;
typedef long long int lint;

int main() {
  ulint N;
  cin >> N;

  vector<ulint> A;

  for (int i = 0; i < N; ++i) {
    ulint a;
    cin >> a;
    A.push_back(a);
  }

  vector<ulint> C(N, (ulint)-1);

  for (int i = N - 1; i >= 0; --i) {
    // TODO: binary search here.
    auto it = upper_bound(C.begin(), C.end(), A[i]);
    // int j = 0;
    // while (A[i] >= C[j]) {
    //   ++j;
    // }
    *it = A[i];
    // C[j] = A[i];
  }

  // cout << "C= " << endl;
  // for (int i = 0; i < N; ++i) {
  //   cout << C[i] << " ";
  // }
  // cout << endl;

  auto it = find(C.begin(), C.end(), (ulint)-1);

  ulint val = it - C.begin();

  cout << val << endl;
}
Problem p02973 (C++)

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) { cin >> A.at(i); }
  // やり方
  // (i) ((1, 4), (2), (0)) みたいに単調増加する数列の入れ物を持つ,
  // 最大値だけ簡単に取り出せるようにしておくことが大事
  // - どの最大値よりも低いものが来た場合は，新しい組が生成
  // - そうじゃない場合は，ぎりぎり大きいものを使う
  // (上の例では，3が来たら(0)側ではなく(2)側に) あ，完全に二分探索やん,,

  vector<ll> x;
  rep(i, N) {
    if (x.size() == 0) {
      x.push_back(A.at(i));
    } else {
      if (A.at(i) <= *min_element(x.begin(), x.end())) {
        // 最小なので先頭に入れる
        x.insert(x.begin(), A.at(i));
      } else {
        // 二分探索で座標を得て入れ替え
        // (2, 4) に3を入れるなら 2 -> 3
        auto pos = lower_bound(x.begin(), x.end(), A.at(i));
        x[distance(x.begin(), pos) - 1] = A.at(i);
      }
    }
  }
  cout << x.size() << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}


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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) { cin >> A.at(i); }
  // やり方
  // (i) ((1, 4), (2), (0)) みたいに単調増加する数列の入れ物を持つ,
  // 最大値だけ簡単に取り出せるようにしておくことが大事
  // - どの最大値よりも低いものが来た場合は，新しい組が生成
  // - そうじゃない場合は，ぎりぎり大きいものを使う
  // (上の例では，3が来たら(0)側ではなく(2)側に) あ，完全に二分探索やん,,

  vector<ll> x;
  rep(i, N) {
    if (x.size() == 0) {
      x.push_back(A.at(i));
    } else {
      if (A.at(i) <= x.at(0)) {
        // 最小なので先頭に入れる
        x.insert(x.begin(), A.at(i));
      } else {
        // 二分探索で座標を得て入れ替え
        // (2, 4) に3を入れるなら 2 -> 3
        auto pos = lower_bound(x.begin(), x.end(), A.at(i));
        x[distance(x.begin(), pos) - 1] = A.at(i);
      }
    }
  }
  cout << x.size() << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 100001;

int main() {
  int N;
  int A[MAX];
  deque<int> color;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < N; i++) {
    int index = lower_bound(color.begin(), color.end(), A[i]) - color.begin();

    if (index == 0) {
      color.push_front(A[i]);
    } else {
      color[i - 1] = A[i];
    }
  }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

const int MAX = 100001;

int main() {
  int N;
  int A[MAX];
  deque<int> color;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < N; i++) {
    int index = lower_bound(color.begin(), color.end(), A[i]) - color.begin();

    if (index == 0) {
      color.push_front(A[i]);
    } else {
      color[index - 1] = A[i];
    }
  }

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

  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, a, b[100005];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; n++) {
    scanf("%d", &a);
    a++;
    *(lower_bound(b, b + n, a) - 1) = a;
  }
  printf("%d\n", n - (lower_bound(b, b + n, 1) - b));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a, b[100005];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    a++;
    *(lower_bound(b, b + n, a) - 1) = a;
  }
  printf("%d\n", n - (lower_bound(b, b + n, 1) - b));
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) (v).begin(), (v).end()
#define siz(v) (ll)(v).size()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

signed main() {
  ll n;
  cin >> n;
  multiset<ll, greater<ll>> t; // 見たならtrue
  rep(i, n) {
    ll a;
    cin >> a;
    auto itr = upper_bound(all(t), a);
    if (t.begin() != itr)
      t.erase(itr);
    t.insert(a);
  }
  cout << siz(t) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) (v).begin(), (v).end()
#define siz(v) (ll)(v).size()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

signed main() {
  ll n;
  cin >> n;
  multiset<ll, greater<ll>> t; // 見たならtrue
  rep(i, n) {
    ll a;
    cin >> a;
    auto itr = t.upper_bound(a);
    if (t.end() != itr)
      t.erase(itr);
    t.insert(a);
  }
  cout << siz(t) << endl;
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
using ll = long long;
using ull = unsigned long long;

int main(void) {
  IOS int n;
  cin >> n;
  vector<int> v;
  int a;
  cin >> a;
  v.emplace_back(a);
  REP(i, n - 1) {
    int x;
    cin >> x;
    if (v.back() >= x) {
      v.emplace_back(x);
    } else {
      auto it = lower_bound(RALL(v), x);
      *it = x;
    }
  }

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
using ll = long long;
using ull = unsigned long long;

int main(void) {
  IOS int n;
  cin >> n;
  vector<int> v;
  int a;
  cin >> a;
  v.emplace_back(a);
  REP(i, n - 1) {
    int x;
    cin >> x;
    if (v.back() >= x) {
      v.emplace_back(x);
    } else {
      auto it = upper_bound(ALL(v), x, greater<int>());
      *it = x;
    }
  }

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

  return 0;
}
Problem p02973 (C++)

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

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

ll A[100005];

int main() {
  ll N;
  cin >> N;
  REP(i, N) { cin >> A[i]; }
  std::multiset<ll> st;
  ll ans = 0;
  REP(i, N) {
    int n = N - i - 1;
    ll m = A[n];
    auto it = lower_bound(st.begin(), st.end(), m + 1);
    if (it == st.end())
      ans++;
    else
      st.erase(it);
    st.insert(m);
  }
  cout << ans << endl;
  return 0;
}

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

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

ll A[100005];

int main() {
  ll N;
  cin >> N;
  REP(i, N) { cin >> A[i]; }
  std::multiset<ll> st;
  ll ans = 0;
  REP(i, N) {
    int n = N - i - 1;
    ll m = A[n];
    auto it = st.lower_bound(m + 1);
    if (it == st.end())
      ans++;
    else
      st.erase(it);
    st.insert(m);
  }
  cout << ans << endl;
  return 0;
}
Problem p02973 (C++)

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

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

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  int A[N];
  rep(i, N) cin >> A[i];
  vector<int> vec;
  rep(i, N) {
    if (i == 0)
      vec.push_back(A[i]);
    else {
      auto it = lower_bound(vec.begin(), vec.end(), A[i]);
      if (it == vec.begin()) {
        reverse(vec.begin(), vec.end());
        vec.push_back(A[i]);
        reverse(vec.begin(), vec.end());
      } else {
        it--;
        *it = A[i];
      }
    }
  }
  // rep(i, vec.size()) cout << vec[i] << " "; cout << endl;
  cout << vec.size() << endl;
  return 0;
}

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

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

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  int A[N];
  rep(i, N) cin >> A[i];
  vector<int> vec;
  rep(i, N) {
    if (i == 0)
      vec.push_back(A[i]);
    else {
      auto it = lower_bound(vec.begin(), vec.end(), A[i]);
      if (it == vec.begin()) {
        vec.insert(it, A[i]);
      } else {
        it--;
        *it = A[i];
      }
    }
  }
  // rep(i, vec.size()) cout << vec[i] << " "; cout << endl;
  cout << vec.size() << endl;
  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  reverse(vec.begin(), vec.end());
  int INF = 100000000;
  vector<int> dp(N, INF);
  vector<int>::iterator pos;
  int idx;
  for (int i = 0; i < N; i++) {
    pos = upper_bound(dp.begin(), dp.end(), vec.at(i));
    idx = distance(dp.begin(), pos);
    dp.at(idx) = vec.at(i);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    // cout << dp.at(i) << " ";
    if (dp.at(i) != INF) {
      ans = i;
    }
  }
  cout << ans + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  reverse(vec.begin(), vec.end());
  int INF = 1e9 + 10;
  vector<int> dp(N, INF);
  vector<int>::iterator pos;
  int idx;
  for (int i = 0; i < N; i++) {
    pos = upper_bound(dp.begin(), dp.end(), vec.at(i));
    idx = distance(dp.begin(), pos);
    dp.at(idx) = vec.at(i);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    // cout << dp.at(i) << " ";
    if (dp.at(i) != INF) {
      ans = i;
    }
  }
  cout << ans + 1 << endl;
}
Problem p02973 (C++)

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

using namespace std;

typedef long long ll;

#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;

int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int &i : a)
    scanf("%d", &i);
  multiset<int, greater<int>> ans;
  for (int i : a) {
    multiset<int>::iterator it = ans.lower_bound(i);
    if (it != ans.end()) {
      while (*it == i)
        ++it;
      if (it != ans.end())
        ans.erase(it);
    }
    ans.insert(i);
  }
  printf("%d ", ans.size());
  return 0;
}

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

using namespace std;

typedef long long ll;

#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;

int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int &i : a)
    scanf("%d", &i);
  multiset<int, greater<int>> ans;
  for (int i : a) {
    multiset<int>::iterator it = ans.upper_bound(i);
    if (it != ans.end())
      ans.erase(it);
    ans.insert(i);
  }
  printf("%d ", ans.size());
  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 100000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define repp(i, n, s) for (int i = (int)(n); i >= (int)(s); i--)
#define mp make_pair
ll mod = 1000000007;
ll mod2 = 998244353;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, 0, n) cin >> a[i], a[i] *= (-1);
  vector<int> nib;
  rep(i, 0, n) {
    auto j = upper_bound(nib.begin(), nib.end(), a[i]);
    if (j == nib.end()) {
      nib.push_back(a[i]);
    } else {
      *j = a[i];
    }
  }
  cout << nib.size() << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 100000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define repp(i, n, s) for (int i = (int)(n); i >= (int)(s); i--)
#define mp make_pair
ll mod = 1000000007;
ll mod2 = 998244353;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, 0, n) cin >> a[i], a[i] *= (-1);
  vector<int> nib;
  rep(i, 0, n) {
    auto j = upper_bound(nib.begin(), nib.end(), a[i]);
    if (j == nib.end()) {
      nib.push_back(a[i]);
    } else {
      *j = a[i];
    }
  }
  cout << nib.size() << endl;
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <iostream>
#include <limits>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;

int main() {
  int N;
  cin >> N;
  vector<int> Ais;
  for (int i = 0; i < N; i++) {
    int Ai;
    cin >> Ai;
    Ais.push_back(Ai);
  }
  vector<int> vals;
  for (int ai : Ais) {
    if (vals.empty()) {
      vals.push_back(ai);
      continue;
    }
    auto it = partition_point(vals.begin(), vals.end(),
                              [ai](int val) { return val >= ai; });
    if (it == vals.end()) {
      vals.push_back(ai);
    } else {
      it = prev(it);
      *it = ai;
    }
  }
  cout << vals.size() << endl;
}


Fixed submission:
//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <iostream>
#include <limits>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;

int main() {
  int N;
  cin >> N;
  vector<int> Ais;
  for (int i = 0; i < N; i++) {
    int Ai;
    cin >> Ai;
    Ais.push_back(Ai);
  }
  vector<int> vals;
  for (int ai : Ais) {
    if (vals.empty()) {
      vals.push_back(ai);
      continue;
    }
    auto it = partition_point(vals.begin(), vals.end(),
                              [ai](int val) { return val >= ai; });
    if (it == vals.end()) {
      vals.push_back(ai);
    } else {
      *it = ai;
    }
  }
  cout << vals.size() << endl;
}
Problem p02973 (C++)

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

using namespace std;
using LL = long long;
using P = pair<int, int>;

#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

const int INF = (int)1e9;
const LL INFL = (LL)1e18;
const int MOD = 1e9 + 7;

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

  int n;
  cin >> n;
  vector<int> a(n);
  REP(i, n) { cin >> a[i]; }

  multiset<int> que;
  que.insert(a[0]);
  FOR(i, 1, n) {
    auto itr = lower_bound(all(que), a[i]);
    if (itr == que.begin()) {
      que.insert(a[i]);
    } else {
      itr--;
      que.erase(itr);
      que.insert(a[i]);
    }
  }
  cout << que.size() << endl;
}

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

using namespace std;
using LL = long long;
using P = pair<int, int>;

#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

const int INF = (int)1e9;
const LL INFL = (LL)1e18;
const int MOD = 1e9 + 7;

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

  int n;
  cin >> n;
  vector<int> a(n);
  REP(i, n) { cin >> a[i]; }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
// #include<unordered_set>

using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
st;*/

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

#define pb push_back
#define ld long double

mt19937 rnd(51);

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    if (a.size() == 0) {
      a.pb(p);
    } else {
      if (a.back() >= p) {
        a.pb(p);
      } else {
        int l = -1, r = n;
        while (r - l > 1) {
          int mid = (r + l) / 2;
          if (a[mid] < p) {
            r = mid;
          } else {
            l = mid;
          }
        }
        a[r] = p;
      }
    }
  }
  cout << a.size() << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
// #include<unordered_set>

using namespace std;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
st;*/

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

#define pb push_back
#define ld long double

mt19937 rnd(51);

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    if (a.size() == 0) {
      a.pb(p);
    } else {
      if (a.back() >= p) {
        a.pb(p);
      } else {
        int l = -1, r = a.size();
        while (r - l > 1) {
          int mid = (r + l) / 2;
          if (a[mid] < p) {
            r = mid;
          } else {
            l = mid;
          }
        }
        a[r] = p;
      }
    }
  }
  cout << a.size() << endl;
  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int N;

int main() {
  cin >> N;

  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  // 各部分増加列の最大値を格納

  vector<int> l;
  l.push_back(A[N - 1]);
  for (int i = N - 2; i >= 0; i--) {
    sort(l.begin(), l.end());

    // 一番でかいやつ以上なら、新たに部分草加列を作成
    if (A[i] >= l[l.size() - 1]) {
      l.push_back(A[i]);
      continue;
    } else if (A[i] < l[0]) {
      l[0] = A[i];
      continue;
    }

    // 追加可能な中で一番最小値の小さい部分増加列に追加
    auto it = upper_bound(l.begin(), l.end(), A[i]);
    *it = A[i];
  }

  cout << l.size();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int N;

int main() {
  cin >> N;

  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  // 各部分増加列の最大値を格納

  vector<int> l;
  l.push_back(A[N - 1]);
  for (int i = N - 2; i >= 0; i--) {

    // 一番でかいやつ以上なら、新たに部分草加列を作成
    if (A[i] >= l[l.size() - 1]) {
      l.push_back(A[i]);
      continue;
    } else if (A[i] < l[0]) {
      l[0] = A[i];
      continue;
    }

    // 追加可能な中で一番最小値の小さい部分増加列に追加
    auto it = upper_bound(l.begin(), l.end(), A[i]);
    *it = A[i];
  }

  cout << l.size();

  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long int ll;

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

ll bin(vector<ll> vec, ll s, ll e, ll v) {
  if (e - s > 1) {
    ll mid = ((s + e) / 2);
    if (vec[mid] >= v) {
      return bin(vec, mid, e, v);
    } else {
      return bin(vec, s, mid, v);
    }
  } else {
    return e;
  }
}

int main(int argc, const char *argv[]) {

  ll N;
  cin >> N;
  vector<ll> maximums = vector<ll>();
  ll count = 0;

  for (ll i = 0; i < N; i++) {
    ll data;
    cin >> data;
    if (count == 0 || maximums[count - 1] >= data) {
      maximums.push_back(data);
      count++;
      continue;
    } else if (maximums[0] < data) {
      maximums[0] = data;
      continue;
    }

    ll index = bin(maximums, 0, count - 1, data);
    maximums[index] = data;
  }

  cout << count;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long int ll;

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

ll bin(vector<ll> &vec, ll s, ll e, ll v) {
  if (e - s > 1) {
    ll mid = ((s + e) / 2);
    if (vec[mid] >= v) {
      return bin(vec, mid, e, v);
    } else {
      return bin(vec, s, mid, v);
    }
  } else {
    return e;
  }
}

int main(int argc, const char *argv[]) {

  ll N;
  cin >> N;
  vector<ll> maximums = vector<ll>();
  ll count = 0;

  for (ll i = 0; i < N; i++) {
    ll data;
    cin >> data;
    if (count == 0 || maximums[count - 1] >= data) {
      maximums.push_back(data);
      count++;
      continue;
    } else if (maximums[0] < data) {
      maximums[0] = data;
      continue;
    }

    ll index = bin(maximums, 0, count - 1, data);
    maximums[index] = data;
  }

  cout << count;

  return 0;
}
Problem p02973 (C++)

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

using ll = long long int;

const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);

const int MAX_N = (int)(1e5 + 5);

int n;
ll a[MAX_N];

multiset<ll> mst;

int main(void) {
  // Here your code !
  scanf("%d", &n);

  for (int i = 0; i < n; ++i) {
    scanf("%lld", &(a[i]));
  }

  mst.insert(INF);

  for (int i = 0; i < n; ++i) {
    // printf("mst: ");
    // for (auto &e : mst) {
    //   printf("%lld ", e);
    // }
    // printf("\n");

    if (*mst.begin() >= a[i] || mst.empty()) {
      mst.insert(a[i]);
      continue;
    }

    auto ptr = upper_bound(mst.begin(), mst.end(), a[i] - 1);
    auto prv = prev(ptr);

    if (prv == mst.end()) {
      mst.insert(a[i]);
      continue;
    }

    mst.erase(prv);
    mst.insert(a[i]);
  }

  printf("%d\n", mst.size() - 1);

  return 0;
}


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

using ll = long long int;

const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);

const int MAX_N = (int)(1e5 + 5);

int n;
ll a[MAX_N];

multiset<ll> mst;

int main(void) {
  // Here your code !
  scanf("%d", &n);

  for (int i = 0; i < n; ++i) {
    scanf("%lld", &(a[i]));
  }

  mst.insert(INF);

  for (int i = 0; i < n; ++i) {
    // printf("mst: ");
    // for (auto &e : mst) {
    //   printf("%lld ", e);
    // }
    // printf("\n");

    if (*mst.begin() >= a[i] || mst.empty()) {
      mst.insert(a[i]);
      continue;
    }

    auto ptr = mst.upper_bound(a[i] - 1);
    auto prv = prev(ptr);

    if (prv == mst.end()) {
      mst.insert(a[i]);
      continue;
    }

    mst.erase(prv);
    mst.insert(a[i]);
  }

  printf("%d\n", mst.size() - 1);

  return 0;
}
Problem p02973 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <set>
using namespace std;
int main() {
  int N;
  cin >> N;
  int A[100010] = {};
  for (int i = 0; i < N; i++)
    cin >> A[i];
  multiset<int> S;
  S.insert(-1145141919);
  S.insert(1145141919);
  S.insert(A[0]);
  for (int i = 1; i < N; i++) {
    auto it = S.lower_bound(A[i]);
    it--;
    if (*it == -1145141919)
      S.insert(A[i]);
    else {
      S.erase(S.lower_bound(A[i])--);
      S.insert(A[i]);
    }
  }
  cout << (int)S.size() - 2 << endl;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <set>
using namespace std;
int main() {
  int N;
  cin >> N;
  int A[100010] = {};
  for (int i = 0; i < N; i++)
    cin >> A[i];
  multiset<int> S;
  S.insert(-1145141919);
  S.insert(1145141919);
  S.insert(A[0]);
  for (int i = 1; i < N; i++) {
    auto it = S.lower_bound(A[i]);
    it--;
    if (*it == -1145141919)
      S.insert(A[i]);
    else {
      it = S.erase(it);
      S.insert(A[i]);
    }
  }
  cout << (int)S.size() - 2 << endl;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

int main() {
  ll n;
  cin >> n;

  vector<ll> A(n), B;

  for (auto &a : A)
    cin >> a;

  for (auto i = A.rbegin(); i != A.rend(); i++) {
    auto a = (*i);
    auto j = upper_bound(B.begin(), B.end(), a);
    if (j == B.end())
      B.push_back(a);
    else
      (*j) = a;
    sort(B.begin(), B.end());
  }

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

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

int main() {
  ll n;
  cin >> n;

  vector<ll> A(n), B;

  for (auto &a : A)
    cin >> a;

  for (auto i = A.rbegin(); i != A.rend(); i++) {
    auto a = (*i);
    auto j = upper_bound(B.begin(), B.end(), a);
    if (j == B.end())
      B.push_back(a);
    else
      (*j) = a;
  }

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

  return 0;
}
Problem p02973 (C++)

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

using namespace std;
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  multiset<ll> x;
  for (int i = 0; i < n; ++i) {
    ll a;
    cin >> a;
    auto it = lower_bound(x.begin(), x.end(), a);
    if (it == x.begin()) {
      x.insert(it, a);
    } else {
      x.erase(--it);
      x.insert(++it, a);
    }
  }
  cout << x.size() << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  multiset<ll> x;
  for (int i = 0; i < n; ++i) {
    ll a;
    cin >> a;
    auto it = x.lower_bound(a);
    if (it == x.begin()) {
      x.insert(it, a);
    } else {
      x.erase(--it);
      x.insert(++it, a);
    }
  }
  cout << x.size() << endl;
  return 0;
}
Problem p02973 (C++)

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

const ll INF = 1LL << 60;

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

  multiset<ll> st;
  rep(i, N) {
    auto iter = lower_bound(st.begin(), st.end(), A[i]);

    if (iter == st.begin()) {
      st.insert(A[i]);
    } else {
      // 置き換え
      iter--;
      st.erase(iter);

      st.insert(A[i]);
    }
  }
  cout << st.size() << endl;
}

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

const ll INF = 1LL << 60;

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

  multiset<ll> st;
  rep(i, N) {
    // auto iter = lower_bound(st.begin(), st.end(), A[i]);
    auto iter = st.lower_bound(A[i]);

    if (iter == st.begin()) {
      st.insert(A[i]);
    } else {
      // 置き換え
      iter--;
      st.erase(iter);

      st.insert(A[i]);
    }
  }
  cout << st.size() << endl;
}
Problem p02973 (C++)

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

#ifdef LOCAL
#include <contest/local.hpp>
#else
#define LOG(...)                                                               \
  do {                                                                         \
  } while (false)
#endif

#define REP(i, n)                                                              \
  for (decay_t<decltype(n)> i = 0, i##__len = (n); i < i##__len; ++i)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

auto main() -> int {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  unsigned n;
  cin >> n;

  multiset<int> group;
  vector<int> a;

  REP(i, n) {
    int value;
    cin >> value;
    a.emplace_back(value);
  }

  auto i = a.begin();
  group.insert(*i);

  for (++i; i != a.end(); ++i) {
    auto &&elem = *i;
    auto ite = lower_bound(group.begin(), group.end(), elem);
    if (ite != group.begin()) {
      --ite;
      group.erase(ite);
    }
    group.insert(elem);
#ifdef LOCAL
    cerr << "dump: ";
    for (auto &&eeeeee : group) {
      cerr << eeeeee << " ";
    }
    cerr << endl;
#endif
  }
  cout << group.size() << endl;
}


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

#ifdef LOCAL
#include <contest/local.hpp>
#else
#define LOG(...)                                                               \
  do {                                                                         \
  } while (false)
#endif

#define REP(i, n)                                                              \
  for (decay_t<decltype(n)> i = 0, i##__len = (n); i < i##__len; ++i)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

auto main() -> int {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  unsigned n;
  cin >> n;

  multiset<int> group;
  vector<int> a;

  REP(i, n) {
    int value;
    cin >> value;
    a.emplace_back(value);
  }

  auto i = a.begin();
  group.insert(*i);

  for (++i; i != a.end(); ++i) {
    auto &&elem = *i;
    auto ite = group.lower_bound(elem);
    if (ite != group.begin()) {
      --ite;
      group.erase(ite);
    }
    group.insert(elem);
#ifdef LOCAL
    cerr << "dump: ";
    for (auto &&eeeeee : group) {
      cerr << eeeeee << " ";
    }
    cerr << endl;
#endif
  }
  cout << group.size() << endl;
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int M = 100001;
int n, A[M], ans;
multiset<int> s;
multiset<int>::iterator it;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &A[i]);
  for (int i = 1; i <= n; i++) {
    if (!s.size()) {
      s.insert(A[i]);
      ans++;
      continue;
    }
    it = lower_bound(s.begin(), s.end(), A[i]);
    if (it == s.begin()) {
      s.insert(A[i]);
      ans++;
    } else {
      it--;
      s.erase(it);
      s.insert(A[i]);
    }
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int M = 100001;
int n, A[M], ans;
multiset<int> s;
multiset<int>::iterator it;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &A[i]);
  for (int i = 1; i <= n; i++) {
    if (!s.size()) {
      s.insert(A[i]);
      ans++;
      continue;
    }
    it = s.lower_bound(A[i]);
    if (it == s.begin()) {
      s.insert(A[i]);
      ans++;
    } else {
      it--;
      s.erase(it);
      s.insert(A[i]);
    }
  }
  printf("%d\n", ans);
}
Problem p02973 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <set>
using namespace std;

int main() {
  long long n;
  cin >> n;

  long long a[n];

  for (long long i = 0; i < n; i++)
    cin >> a[i];

  multiset<long long> T;

  for (long long i = 0; i < n; i++) {
    auto it = lower_bound(T.begin(), T.end(), a[i]);

    if (it != T.begin())
      T.erase(--it);
    T.insert(a[i]);
  }

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

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

int main() {
  long long n;
  cin >> n;

  long long a[n];

  for (long long i = 0; i < n; i++)
    cin >> a[i];

  multiset<long long> T;

  for (long long i = 0; i < n; i++) {
    auto it = T.lower_bound(a[i]);

    if (it != T.begin())
      T.erase(--it);
    T.insert(a[i]);
  }

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