DP Solution | C++
c -> Which array we\'re on. If 1, then nums2, else nums1.\nstore indices of same elements in a map.\n\nA simple easy to understand DP:\n```\nclass Solution {\np
c -> Which array we\'re on. If 1, then nums2, else nums1.\nstore indices of same elements in a map.\n\nA simple easy to understand DP:\n```\nclass Solution {\npublic:\n    map<int, int> mp1, mp2;\n    int n, m;\n    vector<int> nums1,nums2;\n    \n    long long int dp[100002][2];\n    int mod =1e9+7;\n    \n    long long int solve(int i, int c){\n        if(c==0 && i>=n) return 0;\n        if(c==1 && i>=m) return 0;\n        \n        if(dp[i][c]!=-1) return dp[i][c];\n        \n        if(c){\n            if(mp1[nums2[i]]){\n                dp[i][c]=max(nums2[i]+solve(mp1[nums2[i]],c^1), nums2[i]+solve(i+1,c));\n            }\n            else {dp[i][c]=(long long int)nums2[i]+solve(i+1,c); }\n        }\n        \n        else{\n            if( mp2[nums1[i]]){\n                dp[i][c]=max(nums1[i]+solve(mp2[nums1[i]],c^1), nums1[i]+solve(i+1,c));\n            }\n            else {dp[i][c]=(long long int)nums1[i]+solve(i+1,c);}\n        }\n        return dp[i][c];\n    }\n    \n    int maxSum(vector<int>& nums1, vector<int>& nums2) {\n        n=nums1.size();\n        m=nums2.size();\n        this->nums1 = nums1;\n        this->nums2 = nums2;\n        memset(dp,-1,sizeof dp);\n        \n        for(int i=0;i<n;i++){\n            mp1[nums1[i]] = i+1;\n        }\n        for(int i=0;i<m;i++){\n            mp2[nums2[i]]=i+1;\n        }\n        \n        long long int ans=max(solve(0,0), solve(0,1));\n        \n        return ans%mod;\n    }\n};
['Dynamic Programming']