Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //#include <bits/stdc++.h>
- #include <fstream>
- #include <string>
- #include <set>
- #include <set>
- #define MOD 1000000007
- #define ll long long
- using namespace std;
- int n;
- int arr1[100005];
- int arr2[100005];
- int memo[100005][2];
- int dp (int at, int where){
- if(at==n){
- return 0;
- }
- if(memo[at][where] != -1) return memo[at][where];
- int res=0;
- if(where == 0 and at+1<n){
- if(arr1[at+1]>=arr1[at]){
- res = max(res,dp(at+1,0)+1);
- }
- if(arr2[at+1]>=arr1[at]){
- res = max(res,dp(at+1,1)+1);
- }
- }
- else if(where == 1 and at+1<n){
- if(arr2[at+1]>=arr2[at]){
- res = max(res,dp(at+1,1)+1);
- }
- if(arr1[at+1]>=arr2[at]){
- res=max(res,dp(at+1,0)+1);
- }
- }
- return memo[at][where] = res;
- }
- int main() {
- cin >> n;
- for(int i =0;i<n;i++){
- cin >> arr1[i];
- }
- for(int i = 0;i<n;i++){
- cin >> arr2[i];
- }
- memset(memo, -1, sizeof memo);
- int res = 0;
- for(int i= 0;i<n;i++){
- res = max(res ,max(dp(i,0), dp(i,1)));
- }
- cout << res+1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement