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>
  #pragma GCC optimize("O3", "Ofast", "unroll-loops")
  using namespace std;
  const long long maxn = 8e5;
  const long long maxn1 = 4e5;
  vector<long long> z[maxn];
  long long dp[maxn];
  long long a[maxn];
  long long t[4 * maxn];
  long long get(long long node, long long tl, long long tr, long long pos) {
    if (tl > pos || tr <= pos) return (-1e18);
    if ((tr - tl) == 1) return t[node];
    long long tm = (tl + tr) / 2;
    return max({t[node], get(2 * node + 1, tl, tm, pos),
                get(2 * node + 2, tm, tr, pos)});
  }
  void to(long long node, long long tl, long long tr, long long l, long long r,
          long long val) {
    if (tl >= l && tr <= r) {
      t[node] = max(t[node], val);
      return;
    }
    if (tl >= r || tr <= l) {
      return;
    }
    long long tm = (tl + tr) / 2;
    to(2 * node + 1, tl, tm, l, r, val);
    to(2 * node + 2, tm, tr, l, r, val);
  }
  vector<pair<long long, long long> > r;
  void go(long long x) {
    long long val = a[x];
    long long val2 = a[x] - x;
    r.push_back({val2 + maxn1 + 1, dp[x]});
  }
  void upd(long long x) {
    if (a[x] <= (x + 1)) {
      dp[x] = max(dp[x], 1LL);
    }
    long long val = a[x];
    long long val2 = a[x] - x;
    dp[x] = max(dp[x], get(0, 0, maxn, val2 + maxn1) + 1);
  }
  int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    for (long long i = 0; i < 4 * maxn; ++i) {
      t[i] = (-1e18);
    }
    for (long long i = 0; i < maxn; ++i) dp[i] = (-1e18);
    long long n;
    cin >> n;
    for (long long i = 0; i < n; ++i) {
      cin >> a[i];
      z[a[i]].push_back(i);
    }
    vector<pair<long long, long long> > z;
    for (long long i = 0; i < n; ++i) z.push_back({a[i], i});
    sort(z.begin(), z.end());
    for (long long i = 0; i < z.size(); ++i) {
      if (i == 0 || z[i].first != z[i - 1].first) {
        for (auto l : r) {
          to(0, 0, maxn, 0, l.first, l.second);
        }
        r.clear();
      }
      pair<long long, long long> h = z[i];
      upd(h.second);
      go(h.second);
    }
    cout << max(0LL, *max_element(dp, dp + n));
    return 0;
  }
  