Intuitive Top down DP.
\nIDEA\nThink of numbers as graph and each nums1[i] has edge to nums2[i + 1] and nums2[j] where nums1[i] == nums2[j] and vice versa.\n\nNow we need to find max 
```\nIDEA\nThink of numbers as graph and each nums1[i] has edge to nums2[i + 1] and nums2[j] where nums1[i] == nums2[j] and vice versa.\n\nNow we need to find max sum ending at Math.max( nums1[n1 - 1], nums2[n2 - 1])\n\nRecurrence state\nF(i, turn) -> Math.max(F(i - 1, turn)  F(j - 1, 1 - turn) + nums[i] // based of turn\nnums = if turn == 0,  nums1 else  nums2\n\nBase F(i, turn) = 0 if i < 0\n```\n\n```\nclass Solution {\n    private static final int MOD = 1000000007;\n    private long[][] dp;\n    private Map<Integer, Integer> map1;\n    private Map<Integer, Integer> map2;\n    private int[] nums1;\n    private int[] nums2;\n    public int maxSum(int[] nums1, int[] nums2) {\n        // init\n        this.nums1 = nums1;\n        this.nums2 = nums2;\n        int n1 = nums1.length;\n        int n2 = nums2.length;\n        int n = Math.max(n1, n2);\n        dp = new long[2][n];\n        map1 = new HashMap<>();\n        map2 = new HashMap<>();\n        for(int i = 0; i < n1; i++) {\n            map1.put(nums1[i], i);\n        }\n        for(int i = 0; i < n2; i++) {\n            map2.put(nums2[i], i);\n        }\n        for(long[] d : dp) {\n            Arrays.fill(d, -1);\n        }\n        // get maximum ending on both arrays\n        long ans = Math.max(helper(n2 - 1, 1), helper(n1 - 1, 0));\n        return (int) (ans % MOD);\n    }\n    \n    private long helper(int idx, int turn) {\n        if(idx < 0) {\n            return 0;\n        }\n        if(dp[turn][idx] != -1) {\n            return dp[turn][idx];\n        }\n        //System.out.println(idx + " " + turn);\n        Map<Integer, Integer> map;\n        int[] first;\n        int[] second;\n        if(turn == 0) {\n            map = map2;\n            first = nums1;\n            second = nums2;\n        } else {\n            map = map1;\n            first = nums2;\n            second = nums1;\n        }\n        long ans = 0;\n        \n        ans = Math.max(ans, helper(idx - 1, turn) + first[idx]);\n        if(map.containsKey(first[idx])) {\n            ans = Math.max(ans, helper(map.get(first[idx]) - 1, 1 - turn) + first[idx]);\n        }\n        dp[turn][idx] = ans;\n        //System.out.println(idx + " end " + turn);\n        return ans;\n    }\n}\n```
[]