This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef long double ld;
#define N lli(2e6)
#define MOD lli(1e9 + 7)
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0);
#define heps(v) v.begin(),v.end()
typedef vector<lli> vlli;
typedef pair<lli,lli> plli;
typedef pair<lli,plli> pplli;
typedef vector<plli> vplli;
typedef vector<pplli> vpplli;
typedef map<lli,lli> mlli;
lli t,n,m,k;
string str;
vlli a,b;
lli dp[N];
int main(){
    fast_io
    cin >>n;
    for(lli i = 0;i<n;i++){
        cin >> k;
        a.push_back(k);
    }
    for(lli i = 0;i<n;i++){
        cin >> k;
        b.push_back(k);
    }
    lli enf = 0;
    for(lli i = 0;i<n;i++){
        lli sa = -1,so = -1;
        lli sadeg = 0, sodeg = 0;
        
        for(lli j = i;j<n;j++){
            if(b[i] == a[j]){
                sa = j;
                break;
            }
            if(a[j] >= b[i])
                break;
        }
        for(lli j = i;j>=0;j--){
            if(b[i] == a[j]){
                so = j;
                break;
            }
            if(a[j] >= b[i])
                break;
        }
        lli saat = -1;
        if(sa != -1){
            for(lli j = i+1;j<=sa;j++)
                if(b[j] == b[i])
                    sadeg++;
            lli enb = sadeg;
            for(lli j = i;j>=0;j--){
                bool eks = 0;
                if(j == i && a[i] == b[i])
                    eks = 1;
                enb = max(enb, sadeg + dp[j] - eks);
                if(b[j] == b[i])
                    sadeg++;
            }
            enb = max(enb,sadeg);
            saat = enb;
            
        }
        if(so != -1){
            for(lli j = i;j>=so+1;j--)
                if(b[j] == b[i])
                    sodeg++;
            lli enb = sodeg;
            for(lli j = so;j>=0;j--){
                bool eks = 0;
                if(j == so && a[so] == b[so])
                    eks = 1;
                enb = max(enb, sodeg + dp[j] - eks);
                if(b[j] == b[i])
                    sodeg++;
            }
            enb = max(enb,sodeg);
            dp[i] = max(dp[i],enb);
            enf = max(enf,dp[i]);
        }
        if(sa != -1){
            dp[sa] = max(dp[sa],saat);
            enf = max(enf,dp[sa]);
        }
        //cout << so << " " << sa << " " << enf << endl;
    }
    cout << enf << endl;
}
| # | 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... |