C++ DFS+memo
define ll long long int\nclass Solution {\npublic:\n    \n   \n    ll dfsit(int i,vector &dp,unordered_map> &mp){\n        if(mp.count(i)==0){\n            retu
#define ll long long int\nclass Solution {\npublic:\n    \n   \n    ll dfsit(int i,vector<ll> &dp,unordered_map<int,vector<int>> &mp){\n        if(mp.count(i)==0){\n            return i;\n        }\n        \n        ll nxt=0;\n        \n        if(dp[i]!=-1){\n                return dp[i];\n        }\n        \n        for(auto j:mp[i]){\n                nxt=max(nxt,dfsit(j,dp,mp));            \n        }\n        nxt+=i;\n        dp[i]=nxt;\n        \n        return dp[i];\n        \n    }\n    \n    int maxSum(vector<int>& nums1, vector<int>& nums2) {\n        unordered_map<int,vector<int>> mp;\n        vector<ll> dp;\n        int maxi=0;\n        \n        for(int i=0;i<nums1.size();i++){\n            maxi=max(maxi,nums1[i]);\n            if(i+1<nums1.size()){\n            mp[nums1[i]].push_back(nums1[i+1]);}           \n            \n        }\n        for(int i=0;i<nums2.size();i++){\n            maxi=max(maxi,nums2[i]);\n            if(i+1<nums2.size()){\n            mp[nums2[i]].push_back(nums2[i+1]);\n            } \n        }\n            dp.resize(maxi+1,-1);\n        \n        \n        return max(dfsit(nums2[0],dp,mp)%1000000007,dfsit(nums1[0],dp,mp)%1000000007);\n         \n        \n        \n    }\n};
[]