//n <= 100
#include<bits/stdc++.h>
using namespace std;
int dp[5005][5005], a[5005], b[5005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n; cin>>n;
for(int i = 1; i <= n; i++) cin>>a[i];
for(int i = 1; i <= n; i++) cin>>b[i];
int curmaxa = 0;
for(int i = 1; i <= n; i++){
curmaxa = max(curmaxa, a[i]);
if(a[i] == curmaxa) dp[0][i] = 0;
else dp[0][i] = -1e9;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
int curmax = 0;
for(int k = j; k >= 1; k--){
curmax = max(curmax, a[k]);
if(curmax == a[k] || curmax == a[j]) dp[i][j] = max(dp[i][j], dp[i-1][k]);
}
if(b[i] == a[j]) dp[i][j]++;
}
for(int j = 1; j <= n; j++){
int curmax = 0;
for(int k = min(i, j); k <= max(i, j); k++) curmax = max(curmax, a[k]);
if(curmax > a[j]) dp[i][j] = -1e9;
}
}
int ans = 0, curmax = 0;
for(int i = n; i >= 1; i--){
curmax = max(curmax, a[i]);
if(a[i] == curmax) ans = max(ans, dp[n][i]);
}
cout<<ans;
}
# | 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... |