description: Mr. Chanek gives you a sequence a indexed from 1 to n. Define f(a) as the number of indices where a_i = i. 
  
  You can pick an element from the current sequence and remove it, then concatenate the remaining elements together. For example, if you remove the 3-rd element from the sequence [4, 2, 3, 1], the resulting sequence will be [4, 2, 1]. 
  
  You want to remove some elements from a in order to maximize f(a), using zero or more operations. Find the largest possible f(a).
  
  Input
  
  The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the initial length of the sequence.
  
  The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 2 ⋅ 10^5) — the initial sequence a.
  
  Output
  
  Output an integer denoting the largest f(a) that can be obtained by doing zero or more operations.
  
  Examples
  
  Input
  
  
  7
  2 1 4 2 5 3 7
  
  
  Output
  
  
  3
  
  
  Input
  
  
  4
  4 2 3 1
  
  
  Output
  
  
  2
  
  Note
  
  In the first example, f(A) = 3 by doing the following operations.
  
  [2,1,4,2,5,3,7] → [2,1,2,5,3,7] → [1,2,5,3,7] → [1,2,5,3] → [1,2,3]
  
  In the second example, f(A) = 2 and no additional operation is needed.
solution: #include <bits/stdc++.h>
  using namespace std;
  bool isprime(long long k) {
    for (long long i = 2; i <= sqrt(k); i++)
      if (k % i == 0) return false;
    return true;
  }
  long long bm(long long a, long long b, long long mod) {
    if (b == 0) return 1;
    long long t = bm(a, b / 2, mod);
    t = t * t % mod;
    return (b % 2 == 1 ? t * a % mod : t);
  }
  long long inv(long long a, long long mod) { return bm(a, mod - 2, mod); }
  void gay(long long TC) { cout << "Case #" << TC << ": "; }
  struct pe {
    long long v;
    long long id;
  } a[200005];
  long long b[200005];
  bool cmp(pe x, pe y) {
    if (x.v == y.v) return x.id < y.id;
    return x.v < y.v;
  }
  long long d[200005];
  long long cnt;
  long long n;
  bool can(long long k) {
    long long pos;
    cnt = 0;
    for (long long i = 0; i < n; i++) d[i] = 0;
    for (long long i = 1; i < n; i++) {
      if (a[i].v >= 0) {
        pos = i;
        break;
      }
    }
    for (long long i = pos; i < n; i++) {
      if (cnt == 0 || a[i].id > d[cnt]) {
        d[++cnt] = a[i].id;
      } else {
        long long k = lower_bound(d + 1, d + 1 + cnt, a[i].id) - d;
        if (k >= 0) d[k] = a[i].id;
      }
      if (cnt >= k) return true;
    }
    return false;
  }
  void solve(long long TC) {
    cin >> n;
    for (long long i = 1; i <= n; i++) {
      cin >> b[i];
      if (b[i] > n) {
        b[i] = n + 1;
      }
      a[i].v = i - b[i];
      a[i].id = b[i];
    }
    sort(a + 1, a + 1 + n, cmp);
    long long l = 0, r = n;
    n++;
    while (l < r) {
      long long m = (l + r + 1) / 2;
      if (!can(m))
        r = m - 1;
      else
        l = m;
    }
    cout << l << '\n';
  }
  signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long t = 1;
    for (long long i = 1; i <= t; i++) solve(i);
    return 0;
  }
  