C++ solution using set
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
    int n, m;
public:
    int solve(vector<int>& nums1, vector<int>& nums2) {
        vector<long long> pref1(n+1, 0), pref2(m+1, 0);
        pref1[1] = nums1[0]; pref2[1] = nums2[0];

        for(int i=2;i<=n;i++) pref1[i] = pref1[i-1]+nums1[i-1];
        for(int i=2;i<=m;i++) pref2[i] = pref2[i-1]+nums2[i-1];

        unordered_map<int, int> mp1;
        for(int i=0;i<n;i++) mp1[nums1[i]]=i+1;

        int prev1 = 0;
        int prev2 = 0;
        long long res = 0;

        for(int i=0;i<m;i++) {
            if(mp1.find(nums2[i])!=mp1.end()) {
                long long sum1 = pref1[mp1[nums2[i]]-1] - pref1[prev1];
                long long sum2 = pref2[i] - pref2[prev2];

                res += max(sum1, sum2);

                prev1 = mp1[nums2[i]]-1;
                prev2 = i;
            }
        }

        if(nums1[n-1]!=nums2[m-1]) {
            long long sum1 = pref1[n]-pref1[prev1];
            long long sum2 = pref2[m]-pref2[prev2];
            res += max(sum1, sum2);
        }
        return res%1000000007;
    }
    
    int maxSum(vector<int>& nums1, vector<int>& nums2) {
        n = nums1.size();
        m = nums2.size();
        return solve(nums1, nums2);
    }
};
```
['C++']