CSES - Shared codeLink to this code: https://cses.fi/paste/d9ab1dc3493a63706cadc1/
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;

int n;
int a[N];
vector<int> lis;

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
        if (lis.size() == 0 || a[i] > lis.back()) lis.emplace_back(a[i]);
        else *lower_bound(lis.begin(), lis.end(), a[i]) = a[i];
    }
    cout << lis.size() << '\n';
}