You are given two sorted arrays of distinct integers nums1 and nums2.

A valid path is defined as follows:


	Choose array nums1 or nums2 to traverse (from index-0).
	Traverse the current array from left to right.
	If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path).


The score is defined as the sum of uniques values in a valid path.

Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 109 + 7.

&nbsp;
Example 1:

Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9]
Output: 30
Explanation: Valid paths:
[2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10],  (starting from nums1)
[4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10]    (starting from nums2)
The maximum is obtained with the path in green [2,4,6,8,10].


Example 2:

Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100]
Output: 109
Explanation: Maximum sum is obtained with the path [1,3,5,100].


Example 3:

Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10]
Output: 40
Explanation: There are no common elements between nums1 and nums2.
Maximum sum is obtained with the path [6,7,8,9,10].


&nbsp;
Constraints:


	1 &lt;= nums1.length, nums2.length &lt;= 105
	1 &lt;= nums1[i], nums2[i] &lt;= 107
	nums1 and nums2 are strictly increasing.


from itertools import accumulate
class Solution:
    def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
        acc1 = list(accumulate(nums1, initial = 0))
        acc2 = list(accumulate(nums2, initial = 0))
        i, j = len(nums1)-1, len(nums2)-1
        previ, prevj = len(nums1), len(nums2)
        prev_maxscore = 0
        while i >= 0 and j >= 0:
            while i >= 0 and j >= 0 and nums1[i] < nums2[j]:
                j -= 1
            if i >= 0 and j >= 0 and nums1[i] == nums2[j]:
                prev_maxscore += max(acc1[previ]-acc1[i], acc2[prevj]-acc2[j])
                previ, prevj = i, j
                i -= 1
                j -= 1
            while i >= 0 and j >= 0 and nums2[j] < nums1[i]:
                i -= 1
            if  i >= 0 and j >= 0 and nums1[i] == nums2[j]:
                prev_maxscore += max(acc1[previ]-acc1[i], acc2[prevj]-acc2[j])
                previ, prevj = i, j
                i -= 1
                j -= 1
        prev_maxscore += max(acc1[previ]-acc1[0], acc2[prevj]-acc2[0])
        return prev_maxscore % (10**9 + 7)
class Solution {
    public int maxSum(int[] nums1, int[] nums2) {
        long currSum = 0, sum1 = 0, sum2 = 0;
        int i = 0;
        int j = 0;
        while(i < nums1.length && j < nums2.length){
            if(nums1[i] == nums2[j]) {
                currSum += Math.max(sum1, sum2) + nums2[j];
                sum1 = 0;
                sum2 = 0;
                i++;
                j++;
            }
            else if(nums1[i] < nums2[j]){
                sum1 += nums1[i++];
            } else {
                sum2 += nums2[j++];
            }
        }
       
        while(i < nums1.length){
            sum1 += nums1[i++];
        }
        while(j < nums2.length){
            sum2 += nums2[j++];
        }
        return (int)((currSum + Math.max(sum1, sum2)) % 1000000007);
    }
}
#define ll long long
class Solution {
public:
    ll dp1[100005];
    ll dp2[100005];
    
    ll mod=1e9+7;
    
    ll func(vector<int> &nums1 , vector<int> &nums2 , unordered_map< int , int> &mp1,
 unordered_map< int , int> &mp2 , int i1 , int i2 , int n1 , int n2 , bool f1)
    {
        if(i1>=n1 || i2>=n2)
        {
            return 0;
        }
        
        ll sum1=0;
        
        ll sum2=0;
        
        ll ans=0;
        
        if(f1==true)
        {
            if(dp1[i1]!=-1)
            {
                return dp1[i1];
            }
        }
        else
        {
            if(dp2[i2]!=-1)
            {
                return dp2[i2];
            }
        }
        
        if(f1==true)
        {
            sum1=(sum1 + nums1[i1]);
            auto it=mp2.find(nums1[i1]);
            
            sum1=(sum1 + func(nums1 , nums2 , mp1 , mp2 , i1+1 , i2 , n1 , n2 , true));
            
            if(it!=mp2.end())
            {
                int idx=mp2[nums1[i1]];
                sum2=(sum2 + nums2[idx]);
                
                sum2=(sum2 + func(nums1 , nums2 , mp1 , mp2 , i1 , idx+1 , n1 , n2 , false ));
                
            }
            ans=max(sum1 , sum2);
            
            dp1[i1]=ans;
        }
        else
        {
            sum2=(sum2 + nums2[i2]);
            auto it=mp1.find(nums2[i2]);
            
            sum2=(sum2 + func(nums1 , nums2 , mp1 , mp2 , i1 , i2+1 , n1 , n2 , false));
            
            if(it!=mp1.end())
            {
                int idx=mp1[nums2[i2]];
                sum1= (sum1 + nums1[idx]);
                sum1=(sum1 + func(nums1 , nums2 , mp1 , mp2 , idx+1 , i2 , n1 , n2 , true));
            }
            ans=max(sum1 , sum2);
            dp2[i2]=ans;
        }
        
        
        return ans;
        
        
    }
    
    int maxSum(vector<int>& nums1, vector<int>& nums2) {
         
        // at every equal value we can switch 
        
        // the elements from array1 to array2
        
        unordered_map< int , int> mp1 , mp2;
        
        int n1=nums1.size() , n2=nums2.size();
        
        for(int i=0;i<nums1.size();i++)
        {
            mp1[nums1[i]]=i;
        }
        
        for(int i=0;i<nums2.size();i++)
        {
            mp2[nums2[i]]=i;
        }
        
        // start from both array as nums1 & nums2 , nums2 & nums1
        
        memset(dp1 , -1 , sizeof(dp1));
        memset(dp2 , -1 , sizeof(dp2));
        
        bool f1=true;
        
        ll ans1=func(nums1 , nums2 , mp1 , mp2 , 0 , 0 ,n1 , n2 , f1);
        memset(dp1 , -1 , sizeof(dp1));
        memset(dp2 , -1 , sizeof(dp2));
        ll ans2=func(nums2 , nums1 , mp2 , mp1 , 0 , 0 , n2 , n1 , f1);
        
        ans1=ans1%mod;
        ans2=ans2%mod;
        
        return max(ans1 , ans2);
        
    }
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var maxSum = function(nums1, nums2) {
    let MODULO_AMOUNT = 10 ** 9 + 7;
    let result = 0;
    let ptr1 = 0;
    let ptr2 = 0;
    let n1 = nums1.length;
    let n2 = nums2.length;
    let section_sum1 = 0;
    let section_sum2 = 0;
    while(ptr1 < n1 || ptr2 < n2){
        if(ptr1 < n1 && ptr2 < n2 && nums1[ptr1] == nums2[ptr2]){
            result += Math.max(section_sum1, section_sum2) + nums1[ptr1];
            result %= MODULO_AMOUNT;
            section_sum1 = 0;
            section_sum2 = 0;
            ptr1 += 1;
            ptr2 += 1;
            continue;
        }

        if(ptr1 == n1 || (ptr2 != n2 && nums1[ptr1] > nums2[ptr2])){
            section_sum2 += nums2[ptr2];
            ptr2 += 1;
        }else{
            section_sum1 += nums1[ptr1];
            ptr1 += 1;
        }
    }
    result += Math.max(section_sum1, section_sum2);
    return result % MODULO_AMOUNT;
};
Get the Maximum Score