One liner logic,Two pointer,[C++] O(n+m) Time Complexity and O(1) Space
So the One liner logic for this is that if nums1[i]==nums2[j],then the maximum sum you can get till this indexes i,j are \nmax(Sum1[i-1]+nums1[i],Sum2[j-1]+nums
So the One liner logic for this is that if nums1[i]==nums2[j],then the maximum sum you can get till this indexes i,j are \nmax(Sum1[i-1]+nums1[i],Sum2[j-1]+nums2[j]),Now you can replace can replace Sum1[i]=Sum2[j]=max(Sum1[i-1]+nums1[i],Sum2[j-1]+nums2[j]).\nWhere Sum1[i] is maximum sum which you can get till index i in array one.\nWhere Sum2[j] is maximum sum which you can get till index j in array two.\n```\nclass Solution {\npublic:\n    int maxSum(vector<int>& nums1, vector<int>& nums2) {\n        int n=nums1.size(),m=nums2.size(),i,j;\n        long long int l1=0,l2=0;\n        i=0,j=0;\n        while(i<n && j<m)\n        {\n            if(nums1[i]<nums2[j])\n            {\n                l1+=(long long int)nums1[i++];\n            }\n            else if(nums2[j]<nums1[i])\n            {\n                l2+=(long long int)nums2[j++];\n            }\n            else\n            {\n                l1+=(long long int)nums1[i];\n                l2+=(long long int)nums1[i];\n                long long int a=l1,b=l2;\n                l1=max(a,b);\n                l2=max(b,a);\n                i++;\n                j++;\n            }\n        }\n        while(i<n)\n        {\n            l1+=nums1[i++];\n        }\n        while(j<m)\n        {\n            l2+=nums2[j++];\n        }\n        return max(l1,l2)%1000000007;\n    }\n};\n```\nBe carefull of overflow!!
[]