#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define ins insert
#define pb push_back
#define F first
#define S second
const int N = 2e5+4, M = 5e5 + 7;
const int mod = 1e9 + 7;
int a[N], b[N], dp[N], l[N], r[N];
void solve(){
    int n;
    cin>>n;
    for(int i = 1; i <= n; i++) cin>>a[i];
    for(int i = 1; i <= n; i++) cin>>b[i];
    {
        stack<int>s;
        for(int i = 1; i <= n; i++){
            while(s.size() && a[s.top()] <= a[i]) s.pop();
            if(s.size()) l[i] = s.top();
            else l[i] = 0;
            s.push(i);
        }
    }
    {
        stack<int>s;
        for(int i = n; i >= 1; i--){
            while(s.size() && a[s.top()] <= a[i]) s.pop();
            if(s.size()) r[i] = s.top();
            else r[i] = n + 1;
            s.push(i);
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        int mx = 0;
        for(int j = 1; j <= n; j++){
            mx = max(mx, dp[j]);
            if(a[j] == b[i] && l[j] < i && i < r[j]) dp[j] = mx + 1;
            ans = max(ans, dp[j]);
        }
    }
    cout<<ans;
}
main(){
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr); 
    int t = 1;
    //cin>>t; 
    for(int i = 1; i <= t; i++){
        //cout<<"Case "<<i<<": ";
        solve();
    }
}
컴파일 시 표준 에러 (stderr) 메시지
exam.cpp:46:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   46 | main(){
      | ^~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |