Language: C++

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    
    int ok = n;
    int ng = 0;
    while(ok-ng>1){
        int mid = (ok+ng)/2;
        stack<pair<int,int> > s;
        s.push(MP(1,1));
        int p;
        int c;
        bool flag = 0;
        int cnt = 0;
        rep(i,n){
            if(i==0){
                if(a[i]==1)continue;
                p = a[i];
                c = 1;
                s.push(MP(p,c));
            }else{
                    auto x = s.top();
                    int id = x.first;
                    int ch = x.second;
                    
                    //cerr << id << " " << ch << " " << a[i] << endl;
                    if(id<a[i]){
                        s.push(MP(a[i],1));
                    }else{
                        while(id>a[i]){
                            s.pop();
                            auto y = s.top();
                            id = y.first;
                            ch = y.second;
                        }
                        if(id==1&&ch==mid){
                            flag = 1;
                        }else if(id==a[i]){
                            s.pop();
                            if(ch>=mid){
                                
                                if(mid==1){
                                    flag = 1;
                                }
                                while(!s.empty()){
                                    auto y = s.top();
                                    if(id==y.first+1){
                                        s.pop();
                                        if(y.second>=mid){
                                            if(y.first==1){
                                                flag = 1;
                                                break;
                                            }
                                            id--;
                                            continue;
                                        }else{
                                            s.push(MP(y.first,y.second+1));
                                            s.push(MP(a[i],1));
                                            break;
                                        }
                                    }else if(id<y.first){
                                        assert(0);
                                    }else{
                                        s.push(MP(id-1,2));
                                        s.push(MP(a[i],1));
                                        break;
                                    }
                                }
                                if(s.empty()){
                                    flag = 1;
                                    break;
                                }
                            }else{
                                s.push(MP(id,ch+1));
                                
                            }
                        }else{
                            if(mid==1){
                                flag = 1;
                            }
                            s.push(MP(a[i],2));
                        }
                    }
                }
            }
            if(flag){
                break;
            }
        }
        //cerr << mid << " " << flag << endl;
        if(flag){
            ng = mid;
        }else{
            ok = mid;
        }
    }
    cout << ok << endl;
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:123:12: error: 'flag' was not declared in this scope
  123 |         if(flag){
      |            ^~~~
a.cc:124:18: error: 'mid' was not declared in this scope; did you mean 'mod'?
  124 |             ng = mid;
      |                  ^~~
      |                  mod
a.cc:126:18: error: 'mid' was not declared in this scope; did you mean 'mod'?
  126 |             ok = mid;
      |                  ^~~
      |                  mod
a.cc: At global scope:
a.cc:129:5: error: 'cout' does not name a type
  129 |     cout << ok << endl;
      |     ^~~~
a.cc:130:5: error: expected unqualified-id before 'return'
  130 |     return 0;
      |     ^~~~~~
a.cc:131:1: error: expected declaration before '}' token
  131 | }
      | ^