Maximum Sum Path in Two Arrays
Submissions: 1053   Accuracy:

38.35%

  Difficulty: Medium   Marks: 4

Given two sorted arrays such the arrays may have some common elements.
Find the sum of the maximum sum path to reach from beginning of any array to end of any of the two arrays. We can start from any array and switch from one array to another array only at common elements. 

Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains two integers M and N denoting the size of two arrays.
Then two lines follow, first containing M space-separated integers A1, A2, ..., AM denoting the elements of the array and
second containing N space-separated integers A1, A2, ..., AN denoting the elements of the array.

Output:
Print the maximum sum.

Constraints:
1 <= T <= 100
1 <= N <= 100
0 <=A[i]<=100

Example:

Input:
2
8 8
2 3 7 10 12 15 30 34
1 5 7 8 10 15 16 19
3 4
1 2 3
1 1 2 4

Output:
122
8
Explanation:
For sample test case 2, the path will be 1 1 2 4 as we can switch between arrays at common elements .In this case we'll start from first array and then move
to second array to get the maximum sum path.

** For More Input/Output Examples Use 'Expected Output' option **

Author: vaibhav95


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.