DFS Approach, Finding Common Numbers and changing path 
Code

# Code
```java []
import java.util.*;

class Solution {
    private List<List<Integer>> allPaths = new ArrayList<>();
    private List<Integer> p = new ArrayList<>();
    int currSum = 0;
    int ans = Integer.MIN_VALUE;

    public int maxSum(int[] nums1, int[] nums2) {
        HashMap<Integer, int[]> commons = findCommons(nums1, nums2);

        dfs(0, nums1, nums2, commons, 1);
        dfs(0, nums1, nums2, commons, 2);

        return ans;
    }

    private void dfs(int start, int[] nums1, int[] nums2, HashMap<Integer, int[]> commons, int direction) {

        if (direction == 1 && start >= nums1.length) {
            ans = Math.max(ans, currSum);
            return;
        } else if (direction == 2 && start >= nums2.length) {
            ans = Math.max(ans, currSum);
            return;
        }

        if (direction == 1 && commons.containsKey(nums1[start])) {
            p.add(nums1[start]);
            currSum += nums1[start];
            int[] dir = commons.get(nums1[start]);

            if (dir[0] + 1 < nums1.length) {
                dfs(dir[0] + 1, nums1, nums2, commons, 1);
            }
            if (dir[1] + 1 < nums2.length) {
                dfs(dir[1] + 1, nums1, nums2, commons, 2);
            }

            currSum -= nums1[start];
            p.remove(p.size() - 1);
        } else if (direction == 2 && commons.containsKey(nums2[start])) {
            p.add(nums2[start]);
            currSum += nums2[start];
            int[] dir = commons.get(nums2[start]);

            if (dir[0] + 1 < nums1.length) {
                dfs(dir[0] + 1, nums1, nums2, commons, 1);
            }
            if (dir[1] + 1 < nums2.length) {
                dfs(dir[1] + 1, nums1, nums2, commons, 2);
            }

            currSum -= nums2[start];
            p.remove(p.size() - 1);
        } else {
            if (direction == 1) {
                p.add(nums1[start]);
                currSum += nums1[start];
            } else if (direction == 2) {
                p.add(nums2[start]);
                currSum += nums2[start];
            }

            dfs(start + 1, nums1, nums2, commons, direction);
            currSum -= p.get(p.size() - 1);
            p.remove(p.size() - 1);
        }
    }

    private HashMap<Integer, int[]> findCommons(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> nums = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) {
            nums.put(nums1[i], i);
        }

        HashMap<Integer, int[]> commons = new HashMap<>();

        for (int i = 0; i < nums2.length; i++) {
            if (nums.containsKey(nums2[i])) {
                commons.put(nums2[i], new int[]{nums.get(nums2[i]), i});
            }
        }

        return commons;
    }
}
```
['Java']