Maximum sum of increasing order elements from n arrays
Submissions: 1560   Accuracy:

36.02%

  Difficulty: Easy   Marks: 2

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 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
Output:
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.

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

Contributor: Vanshika
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.



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


to report an issue on this page.