C++ , Simple , Memoization , Recursion , Simple
Intuition\n\n\n# Approach\n\n# Complexity\n\n# Code\ncpp []\nclass Solution {\n    int MOD=1e9+7;\n    long long find(int i,bool firstvec,int n,int m,unordered_
# Intuition\n\n\n# Approach\n\n# Complexity\n\n# Code\n```cpp []\nclass Solution {\n    int MOD=1e9+7;\n    long long find(int i,bool firstvec,int n,int m,unordered_map<int,int>&m1,unordered_map<int,int>&m2,vector<int>&nums1,vector<int>&nums2,vector<vector<long long>>&dp){\n        if((firstvec && i>=n) || (!firstvec && i>=m))\n            return 0;\n        if(dp[i][firstvec]!=-1)\n            return dp[i][firstvec];\n        long long ans=0;\n        if(firstvec){\n            ans=max(ans,(nums1[i]+find(i+1,true,n,m,m1,m2,nums1,nums2,dp)));\n            if(m1[nums1[i]]!=-1){\n                ans=max(ans,(nums1[i]+find(m1[nums1[i]]+1,false,n,m,m1,m2,nums1,nums2,dp)));\n            }\n            \n        }else{\n            ans=max(ans,(nums2[i]+find(i+1,false,n,m,m1,m2,nums1,nums2,dp)));\n            if(m2[nums2[i]]!=-1){\n                ans=max(ans,(nums2[i]+find(m2[nums2[i]]+1,true,n,m,m1,m2,nums1,nums2,dp)));\n            }\n        }\n        return dp[i][firstvec]=ans;\n    }\npublic:\n    int maxSum(vector<int>& nums1, vector<int>& nums2) {\n        int n=nums1.size();\n        int m=nums2.size();\n        \n        unordered_map<int,int>m1;\n        unordered_map<int,int>m2;\n        for(int i=0;i<n;i++){\n            m1[nums1[i]]=-1;\n        }\n        for(int j=0;j<m;j++){\n            if(m1.count(nums2[j])){\n                m1[nums2[j]]=j;\n            }\n        }\n\n        for(int j=0;j<m;j++){\n            m2[nums2[j]]=-1;\n        }\n        for(int i=0;i<n;i++){\n            if(m2.count(nums1[i])){\n                m2[nums1[i]]=i;\n            }\n        }\n        int size=max(n,m);\n        vector<vector<long long>>dp(size+1,vector<long long>(2,-1));\n        return max(find(0,true,n,m,m1,m2,nums1,nums2,dp),find(0,false,n,m,m1,m2,nums1,nums2,dp))%MOD;\n    }\n};\n```
['Array', 'Hash Table', 'Two Pointers', 'Dynamic Programming', 'Greedy', 'C++']