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>
#define int long long
using namespace std;
const int N = 5e3 + 42, INF = 1e18 + 42;
int n, a[N], b[N], nb[N][N], cost[N][N], dp[N];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    for(int i = 0; i < n; i++)
        cin >> b[i];
    
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++) {
            nb[i][j+1] = nb[i][j];
            if(a[i] == b[j])
                nb[i][j+1]++;
        }
        
    for(int i = 0; i < n; i++) {
        int imax = i;
        for(int j = i+1; j <= n; j++) {
            if(a[imax] < a[j-1])
                imax = j-1;
            cost[i][j] = nb[imax][j] - nb[imax][i];
        }
    }
    
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < i; j++)
            dp[i] = max(dp[i], dp[j] + cost[j][i]);
    cout << dp[n];
}
| # | 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... |