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>
  const long long MAXN = 200005;
  const long long INF = 10000000;
  long long n;
  struct node {
    long long val = -INF;
    node *L = NULL, *R = NULL;
  };
  void update(long long l, long long r, node* root, long long x, long long t) {
    if (x < l || x > r) {
      return;
    }
    if (l == x && l == r) {
      root->val = t;
      return;
    }
    long long mid = (l + r) / 2;
    long long maxi = -INF;
    if (x <= mid) {
      if (root->L == NULL) {
        root->L = new node;
      }
      update(l, mid, root->L, x, t);
    } else {
      if (root->R == NULL) {
        root->R = new node;
      }
      update(mid + 1, r, root->R, x, t);
    }
    if (root->L != NULL) {
      maxi = std::max(root->L->val, maxi);
    }
    if (root->R != NULL) {
      maxi = std::max(root->R->val, maxi);
    }
    root->val = maxi;
  }
  long long findd(long long l, long long r, node* root, long long x,
                  long long y) {
    if (x > r || y < l) {
      return -INF;
    }
    if (x <= l && r <= y) {
      return root->val;
    }
    long long mid = (l + r) / 2;
    long long maxi = -INF;
    if (root->L != NULL) {
      maxi = std::max(maxi, findd(l, mid, root->L, x, y));
    }
    if (root->R != NULL) {
      maxi = std::max(maxi, findd(mid + 1, r, root->R, x, y));
    }
    return maxi;
  }
  struct seg {
    std::vector<node> arr;
    void upd(long long a, long long b, long long v, long long id = 0,
             long long l = 0, long long r = n - 1) {
      update(0, MAXN, &arr[id], b, v);
      if (l == r) return;
      if (a <= ((l + r) / 2))
        upd(a, b, v, (id + 1), l, ((l + r) / 2));
      else
        upd(a, b, v, (id + 2 * (((l + r) / 2) - l + 1)), ((l + r) / 2) + 1, r);
    }
    long long maxs(long long x, long long y, long long z, long long id = 0,
                   long long l = 0, long long r = n - 1) {
      if (y < l || r < x) return -INF;
      if (x <= l && r <= y) {
        long long ans = findd(0, MAXN, &arr[id], 0, z);
        return ans;
      }
      return std::max(maxs(x, y, z, (id + 1), l, ((l + r) / 2)),
                      maxs(x, y, z, (id + 2 * (((l + r) / 2) - l + 1)),
                           ((l + r) / 2) + 1, r));
    }
  };
  seg tree;
  signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    long long N;
    std::cin >> N;
    n = N + 1;
    std::pair<long long, long long> help[N];
    tree.arr = std::vector<node>(2 * n - 1);
    for (long long i = 0; i < N; i++) {
      std::cin >> help[i].first;
      help[i].second = -i - 1;
    }
    std::sort(help, help + N);
    long long best = 0;
    tree.upd(0, 0, 0);
    for (long long i = 0; i < N; i++) {
      help[i].second *= -1;
      if (help[i].first > help[i].second) continue;
      long long cur =
          tree.maxs(0, help[i].second, help[i].second - help[i].first) + 1;
      best = std::max(best, cur);
      tree.upd(help[i].second, help[i].second - help[i].first, cur);
    }
    std::cout << best << std::endl;
    return 0;
  }
  