[Java/C++/Python] Two Pointers, O(1) Space
Intuition\nWe must take the common elements and won\'t miss them;\nAnd there will be two path between the common elements,\nand we will take and only take one p
## **Intuition**\nWe must take the common elements and won\'t miss them;\nAnd there will be two path between the common elements,\nand we will take and only take one path.\n\nWe calculate the sum of both path, and take the bigger one.\n<br>\n\n## **Explanation**\nSo we apply two points solutions,\nand always take the step in the smaller element.\n\nIf two elements are the same,\nwe compare the accumulated sum in the both paths,\nand we pick the bigger one.\n<br>\n\n## **Complexity**\nTime `O(N)`\nSpace `O(1)`\n<br>\n\n**Java:**\n```java\n    public int maxSum(int[] A, int[] B) {\n        int i = 0, j = 0, n = A.length, m = B.length;\n        long a = 0, b = 0, mod = (long)1e9 + 7;\n        while (i < n || j < m) {\n            if (i < n && (j == m || A[i] < B[j])) {\n                a += A[i++];\n            } else if (j < m && (i == n || A[i] > B[j])) {\n                b += B[j++];\n            } else {\n                a = b = Math.max(a, b) + A[i];\n                i++; j++;\n            }\n        }\n        return (int)(Math.max(a, b) % mod);\n    }\n```\n\n**C++:**\n```cpp\n    int maxSum(vector<int>& A, vector<int>& B) {\n        int i = 0, j = 0, n = A.size(), m = B.size();\n        long a = 0, b = 0, mod = 1e9 + 7;\n        while (i < n || j < m) {\n            if (i < n && (j == m || A[i] < B[j])) {\n                a += A[i++];\n            } else if (j < m && (i == n || A[i] > B[j])) {\n                b += B[j++];\n            } else {\n                a = b = max(a, b) + A[i];\n                i++, j++;\n            }\n        }\n        return max(a, b) % mod;\n    }\n```\n**Python**\n```py\n    def maxSum(self, A, B):\n        i, j, n, m = 0, 0, len(A), len(B)\n        a, b, mod = 0, 0, 10**9 + 7\n        while i < n or j < m:\n            if i < n and (j == m or A[i] < B[j]):\n                a += A[i]\n                i += 1\n            elif j < m and (i == n or A[i] > B[j]):\n                b += B[j]\n                j += 1\n            else:\n                a = b = max(a, b) + A[i]\n                i += 1\n                j += 1\n        return max(a, b) % mod\n```
[]