Given two sorted arrays **A** and **B**. The task is to complete the function **max_path_sum **that takes 4 argument, the first two arguments represent the 2 arrays A[] and B[] and the last two arguments

**Note: **You can switch from one array to another array only at common elements.

**Input:**

The first line of input contains an integer **T **denoting the no of test cases . Then T cases follow.

Each test case contains 3 lines. The first line contains two space separated integers **l1** and **l2** denoting the length of the two sorted array **A** and **B**. In the second line is the space separated values of array A and in the third line are space separated values of array B.

**Output:**

For each test case the output is the max sum obtained in such fashion .

**Constraints:**

1** **<= T <= 100

1 <= N, M <= 10^{3}

1 <= A[], B[] <= 10^{4}

**Example:**

2

5 4

2 3 7 10 12

1 5 7 8

3 3

1 2 4

1 2 7

**Output**

35

10

**Explanation**:

**Testcase 1:** For first test case the path will be 1+5+7+10+12 = 35.

**Testcase 2:** For second test case the path will be 1+2 +7=10.

**Note:**The **Input/Ouput** format and **Example** given are used for system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Author: Shubham Joshi 1

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.

thanuvinu94 | 425 |

the_coder95 | 380 |

RishabhTanwar1 | 280 |

shubham3174 | 208 |

RitikRosan | 202 |

the_coder95 | 1226 |

RishabhTanwar1 | 1000 |

thanuvinu94 | 634 |

tathagat289 | 628 |

themanhasnoname | 620 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4885 |

Quandray | 4547 |

Login to report an issue on this page.