Given **n** arrays of size **m **each. Find maximum sum obtained by selecting a number from each array such that the element selected from the **i-th** array is more than the element selected from **(i-1)-th** array. If maximum sum cannot be obtained then return **0**.

**Input:**

The first line contains an integer **T** denoting the number of test cases. For each test case, the first line contains two integers **n & m** denoting the size of 2-D Array. The next line **n** contains **m **elements each.

**Output:**

For each test case, the output is the maximum sum that can be obtained, if not return **0.**

**Constraints:**

1<=T<=100

1<=n,m<=25

**Example:
Input:**

2

3 4

1 7 4 3

4 2 5 1

9 5 1 8

3 3

9 8 7

6 5 4

3 2 1

18

0

**Explanation:**

1. We can select 4 from the first array, 5 from second array and 9 from the third array.

2. As maximum sum cannot be obtained according to condition, hence 0.

Author: Vanshika_pec

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.

sabka_din_aayega | 317 |

maggiiiii | 248 |

thanuvinu94 | 211 |

Savage_19 | 206 |

Anugrah_kumar | 172 |

Bishnu Dev Panda | 803 |

sabka_din_aayega | 628 |

Anugrah_kumar | 562 |

okayboss | 561 |

maggiiiii | 531 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4925 |

Quandray | 4547 |

Login to report an issue on this page.