C++ Using DFS and Memoization
\n#define mod 1000000007\nclass Solution {\npublic:\n    long long dfs(int u, unordered_map<long long,vector<long long>> &graph, unordered_map<long long,long lo
```\n#define mod 1000000007\nclass Solution {\npublic:\n    long long dfs(int u, unordered_map<long long,vector<long long>> &graph, unordered_map<long long,long long> &maxDist)\n    {\n        if(maxDist.find(u)!=maxDist.end())\n        {\n            return (u+maxDist[u]);\n        }    \n            \n        \n        maxDist[u]=0;\n        \n        for(auto &v: graph[u])\n        {\n            maxDist[u]=max(maxDist[u],dfs(v,graph,maxDist));\n        }\n        \n        return (u+maxDist[u]);\n    }\n    int maxSum(vector<int>& nums1, vector<int>& nums2) {\n        unordered_map<long long,vector<long long>> graph;\n        unordered_map<long long,long long> maxDist;\n        \n        int n=nums1.size();\n        \n        for(int i=0;i<n-1;i++)\n        {\n            graph[nums1[i]].push_back(nums1[i+1]);\n        }\n        \n        n=nums2.size();\n        \n        for(int i=0;i<n-1;i++)\n        {\n            graph[nums2[i]].push_back(nums2[i+1]);\n        }\n        \n         long long val = max(dfs(nums1[0],graph,maxDist),dfs(nums2[0],graph,maxDist));\n        \n        return val%mod;\n        \n        return 0;\n    }\n};\n\n/*\nWe can consider unique integers as nodes of a graph with edges pointing to next member present in nums1 and nums2\nThen perform dfs from starting number of both the arrays and return maximum path sum till last index\n*/\n```
['Depth-First Search', 'C']