C++ 2 solutions
My first solution based on DP\n\nclass Solution {\n    unordered_map<int, int> idx1, idx2;\n    vector<long long> dp1, dp2;\n    int MOD = 1e9+7;\n    \n    lon
My first solution based on DP\n```\nclass Solution {\n    unordered_map<int, int> idx1, idx2;\n    vector<long long> dp1, dp2;\n    int MOD = 1e9+7;\n    \n    long long helper1(const vector<int>& nums1, const vector<int>& nums2, int i) {\n        if (i < 0) return 0;\n        if (dp1[i]) return dp1[i];\n        \n        dp1[i] = helper1(nums1, nums2, i-1);\n        if (idx2.count(nums1[i])) {\n            dp1[i] = max(dp1[i], helper2(nums1, nums2, idx2[nums1[i]]-1));\n        }\n        \n        return dp1[i] = (dp1[i] + static_cast<long long>(nums1[i]));\n    }\n    \n    long long helper2(const vector<int>& nums1, const vector<int>& nums2, int i) {\n        if (i < 0) return 0;\n        if (dp2[i]) return dp2[i];\n        \n        dp2[i] = helper2(nums1, nums2, i-1);\n        if (idx1.count(nums2[i])) {\n            dp2[i] = max(dp2[i], helper1(nums1, nums2, idx1[nums2[i]]-1));\n        }\n        \n        return dp2[i] = (dp2[i] + static_cast<long long>(nums2[i]));\n    }\n    \npublic:\n    int maxSum(vector<int>& nums1, vector<int>& nums2) {        \n        for (int i = 0; i < nums1.size(); ++i)\n            idx1[nums1[i]] = i;\n        for (int i = 0; i < nums2.size(); ++i)\n            idx2[nums2[i]] = i;\n        \n        dp1 = vector<long long>(nums1.size(), 0);\n        dp2 = vector<long long>(nums2.size(), 0);\n        \n        return max(helper1(nums1, nums2, nums1.size()-1), helper2(nums1, nums2, nums2.size()-1)) % MOD;\n    }\n};\n```\n\n2 pointer solution based on merging\n```\nclass Solution {\npublic:\n    int maxSum(vector<int>& nums1, vector<int>& nums2) {\n        long long up = 0, down = 0;\n        int i = 0, j = 0;\n        int m = nums1.size(), n = nums2.size();\n        \n        while (i < m && j < n) {\n            if (nums1[i] < nums2[j]) {\n                up += nums1[i++];\n            } else if (nums2[j] < nums1[i]) {\n                down += nums2[j++];\n            } else {\n                up = down = max(up, down) + nums1[i];\n                ++i, ++j;\n            }\n        }\n        \n        while (i < m) up += nums1[i++];\n        while (j < n) down += nums2[j++];\n        \n        return max(up, down) % 1000000007;\n    }\n};\n```
[]