Solution using mixture of DP (Top-Down) and Two Pointers and HashTable
Approach 1Using 3D Dynamic Programming and Two Pointers.(MEMORY LIMIT EXCEEDED)Intuition
Maintain index of every element in both arrays in two different hashmap

# Approach 1
<!-- Describe your approach to solving the problem. -->
#### Using 3D Dynamic Programming and Two Pointers.
#### (MEMORY LIMIT EXCEEDED)
## Intuition
1) Maintain index of every element in both arrays in two different hashmaps.
2) Initialize a 3D DP array int[][][] dp=new int[a1.len()][a2.len()][3].
Since u can choose any element from both arrays they become your 2 options and since starting is also a condition it will be you third option.
3) Perform memoization , the options you will be having are 
-> continue with same array.
-> change the array.
And take these 2 as 2 options  and cache you result for further computations.
4) Return the result obtained at starting position.

# Complexity
time:- O(N^2)
space:- O(2*N^2)

# Approach 2
 #### Using optimized 1D Dynamic Programming (Accepted)
Follow the same steps as previous approach,

only difference being breaking one 3d array into 2 1d array.
Here we dont need option to get cached because we can consider each array as result of the index for a specific option

DP Array 1 is results of index using option 1
DP Array 2 is results of index using option 2

So this soln gets accepted but not as efficient as other approaches.
But quite innovative :)

# Complexity

# Code
```java []
class Solution {
    // 3D DP APPROACH
    public int backtrack(int[] nums1,int[] nums2,HashMap<Integer,Integer> indexMap1,HashMap<Integer,Integer> indexMap2,int id1,int id2,int[][][] dp,int option){
        if(id1>=nums1.length || id2>=nums2.length){
            return 0;
        }

        if(dp[id1][id2][option]!=-1) return dp[id1][id2][option];

        if(option==0){
            return dp[id1][id2][option]=Math.max(
                backtrack(nums1,nums2,indexMap1,indexMap2,id1,id2,dp,1),
                backtrack(nums1,nums2,indexMap1,indexMap2,id1,id2,dp,2)
            );
        }
        if(option==1){
            int nextIdx=indexMap2.getOrDefault(nums1[id1],-1);

            indexMap1.remove(nums1[id1]);

            int maxVal=backtrack(nums1,nums2,indexMap1,indexMap2,id1+1,id2,dp,1);

            if(nextIdx!=-1){
                indexMap2.remove(nums1[id1]);

                maxVal=Math.max(maxVal,backtrack(nums1,nums2,indexMap1,indexMap2,id1,nextIdx+1,dp,2));

                indexMap2.put(nums1[id1],nextIdx);
            }

            indexMap1.put(nums1[id1],id1);
            return dp[id1][id2][option]=maxVal+nums1[id1];
        }
        else{
            int nextIdx=indexMap1.getOrDefault(nums2[id2],-1);

            indexMap2.remove(nums2[id2]);

            int maxVal=backtrack(nums1,nums2,indexMap1,indexMap2,id1,id2+1,dp,2);

            if(nextIdx!=-1){
                indexMap1.remove(nums2[id2]);

                maxVal=Math.max(maxVal,backtrack(nums1,nums2,indexMap1,indexMap2,nextIdx+1,id2,dp,1));

                indexMap1.put(nums2[id2],nextIdx);

            }
            indexMap2.put(nums2[id2],id2);
            return dp[id1][id2][option]=maxVal+nums2[id2];
        }
    }
    public int maxSum(int[] nums1, int[] nums2) {
        HashMap<Integer,Integer> indexMap1=new HashMap<>();
        HashMap<Integer,Integer> indexMap2=new HashMap<>();

        for(int i=0;i<nums1.length;i++){
            indexMap1.put(nums1[i],i);
        }
        for(int i=0;i<nums2.length;i++){
            indexMap2.put(nums2[i],i);
        }

        int[][][] dp=new int[nums1.length][nums2.length][3];

        for(int i=0;i<nums1.length;i++){
            for(int j=0;j<nums2.length;j++){
                Arrays.fill(dp[i][j],-1);
            }
        }
        
        int ans=backtrack(nums1,nums2,indexMap1,indexMap2,0,0,dp,0);

        return ans;
    }
}
```
```java []
class Solution {
    // COMPROMISED 1D DP APPROACH
    public long backtrack(int[] nums1,int[] nums2,HashMap<Integer,Integer> indexMap1,HashMap<Integer,Integer> indexMap2,int id,long[] dp1,long[] dp2,int option){
        if(option==1){
            if(id>=nums1.length) return 0;

            if(dp1[id]!=-1) return dp1[id];

            int nextIdx=indexMap2.getOrDefault(nums1[id],-1);

            indexMap1.remove(nums1[id]);

            long maxVal=backtrack(nums1,nums2,indexMap1,indexMap2,id+1,dp1,dp2,1);

            if(nextIdx!=-1){
                indexMap2.remove(nums1[id]);

                maxVal=Math.max(maxVal,backtrack(nums1,nums2,indexMap1,indexMap2,nextIdx+1,dp1,dp2,2));

                indexMap2.put(nums1[id],nextIdx);
            }

            indexMap1.put(nums1[id],id);

            return dp1[id]=maxVal+nums1[id];
        }
        else{
            if(id>=nums2.length) return 0;

            if(dp2[id]!=-1) return dp2[id];

            int nextIdx=indexMap1.getOrDefault(nums2[id],-1);

            indexMap2.remove(nums2[id]);

            long maxVal=backtrack(nums1,nums2,indexMap1,indexMap2,id+1,dp1,dp2,2);

            if(nextIdx!=-1){
                indexMap1.remove(nums2[id]);

                maxVal=Math.max(maxVal,backtrack(nums1,nums2,indexMap1,indexMap2,nextIdx+1,dp1,dp2,1));

                indexMap1.put(nums2[id],nextIdx);

            }
            indexMap2.put(nums2[id],id);

            return dp2[id]=maxVal+nums2[id];
        }
    }
    public int maxSum(int[] nums1, int[] nums2) {
        HashMap<Integer,Integer> indexMap1=new HashMap<>();
        HashMap<Integer,Integer> indexMap2=new HashMap<>();

        long[] dp1=new long[nums1.length];
        long[] dp2=new long[nums2.length];

        Arrays.fill(dp1,-1);
        Arrays.fill(dp2,-1);

        for(int i=0;i<nums1.length;i++){
            indexMap1.put(nums1[i],i);
        }
        for(int i=0;i<nums2.length;i++){
            indexMap2.put(nums2[i],i);
        }

        long ans=Math.max(
            backtrack(nums1,nums2,indexMap1,indexMap2,0,dp1,dp2,1),
            backtrack(nums1,nums2,indexMap1,indexMap2,0,dp1,dp2,2)
        );
        int mod=(int)Math.pow(10,9)+7;
        return (int)(ans%mod);
    }
}
```
['Array', 'Hash Table', 'Two Pointers', 'Dynamic Programming', 'Java']