[Java] Greedy O(N+M)
\n    public int maxSum(int[] nums1, int[] nums2) {\n        int mod = 1000000007;\n        long res = 0;\n        int n1 = nums1.length;\n        int n2 = nums
```\n    public int maxSum(int[] nums1, int[] nums2) {\n        int mod = 1000000007;\n        long res = 0;\n        int n1 = nums1.length;\n        int n2 = nums2.length;\n        long[] pSum1 = new long[n1+1];\n        long[] pSum2 = new long[n2+1];\n        for(int i=1; i<=n1; i++){\n            pSum1[i] = pSum1[i-1] + nums1[i-1];\n        }\n        for(int i=1; i<=n2; i++){\n            pSum2[i] = pSum2[i-1] + nums2[i-1];\n        }\n        int p1 = -1;\n        int p2 = -1;\n        int i=0;\n        int j=0;\n        while(i<n1 && j<n2){\n            if(nums1[i]>nums2[j]){\n                j++;\n            }\n            else if(nums1[i]<nums2[j]){\n                i++;\n            }\n            else{\n                long cand1 = pSum1[i+1]-pSum1[p1+1];\n                long cand2 = pSum2[j+1]-pSum2[p2+1];\n                res += Math.max(cand1, cand2);\n                p1 = i;\n                p2 = j;\n                i++;\n                j++;\n            }\n        }\n        long cand1 = pSum1[n1]-pSum1[p1+1];\n        long cand2 = pSum2[n2]-pSum2[p2+1];\n        res += Math.max(cand1, cand2);   \n        return (int) (res%mod);\n    }\n```
[]