# Maximum Candies Codechef Solution

### Problem description.

```Impressed with Agnes' jumping skills, Gru has decided to build a new game for her.
Gru has laid out a grid of N x M tiles, and each tiles contains some (positive) number of candies.
The rules of the games are as following:
- Agnes starts from the tile (0, 0) // 0 based index
- She has to reach the tile (N-1, M-1)
- At a time she can either move in +x direction by 1 tile or in +y direction..
- She cannot  go left or  up .
- She can go from (0, 0) -> (N-1, M-1) only once.
See example below for the movement for more clarity
Now Agnes wants to collect maximum number of candies possible while playing the game. Can you help her out?
Example:
Consider the matrix of 4x3, she starts at (0,0) and collects all candies from (0, 0). Now she can move to (0, 1) or (1, 0).
More generically, allowed moves from (i, j) are
- (i+1, j)  OR
- (i, j+1)
Illegal moves
- (i-1, j)
- (i, j-1)
```

### Input

```First line contains number of tests - T.
Each test consists of following lines:
First line contains two space separated positive integers N and M, representing the grid size
Next N lines represents rows of the grid . Each of the N lines contains M integers. ```

### Output

```Output a single integer that is equal to maximum candies that Agnes can collect.
```

### Constraints

```0 < T < 10
0 < N < 1000
0 < M < 1000
```

Input

```1
2 4
1 2 3 8
3 4 5 6
```

Output
20

### Explanation

Example case 1.

```N = 2, M = 4
First row a = 1 a = 2 a = 3 a = 8
Second row a = 3 a = 4 a = 5 a = 6
Objective reach from a -> a
Take the path:
a -> a -> a -> a-> a
Answer = 1 + 2 + 3 + 8 + 6 = 20```

## Maximum Candies – CodeChef Solution in JAVA

```import java.io.*;
import java.util.*;
class MaximizingCandies {
public static long dp[][];
public static void main(String argsp[]) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int m = in.nextInt();
long arr[][] = new long[n][m];
dp = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = in.nextInt();
dp[i][j] = Long.MIN_VALUE;
}
}
dp[n - 1][m - 1] = arr[n - 1][m - 1];
long answer = solve(arr, 0, 0);
}
in.close();
}
public static long solve(long arr[][], int x, int y) {
if (dp[x][y] > Integer.MIN_VALUE) return dp[x][y];
int n = arr.length;
int m = arr.length;
if (x == n - 1) {
dp[x][y] = arr[x][y] + solve(arr, x, y + 1);
} else if (y == m - 1) {
dp[x][y] = arr[x][y] + solve(arr, x + 1, y);
} else {
dp[x][y] = arr[x][y] + Math.max(solve(arr, x, y + 1), solve(arr, x + 1, y));
}
return dp[x][y];
}
}
```

## Maximum Candies – CodeChef Solution in CPP

```//I_F_A
#include "bits/stdc++.h"
using namespace std;
#define N 1001
long long dp[N][N];
long long arr[N][N];
long long n,m;
long long solve(long long i,long long j){
if(i == n && j == m){
return arr[i][j];
}
if(dp[i][j] == INT_MAX){
long long ans1 = arr[i][j] , ans2 = arr[i][j];
if(i + 1 <= n){
ans1 = ans1 + solve(i+1,j);
}
if(j + 1 <= m){
ans2 = ans2 +solve(i,j+1);
}
dp[i][j] = max(ans1,ans2);
}
return dp[i][j];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc;
cin >> tc;
while(tc--){
cin >> n >> m;
for(long long i=1;i<=n;i++){
for(long long j=1;j<=m;j++){
cin >> arr[i][j];
dp[i][j] = INT_MAX;
}
}
cout << solve(1,1) << endl;
}
}```

## Maximum Candies -CodeChef Solution in Python

```from sys import stdin
def maxCost(cost, m, n):
tc = [[0 for x in range(C)] for x in range(R)]
tc = cost
for i in range(1, m + 1):
tc[i] = tc[i - 1] + cost[i]
for j in range(1, n + 1):
tc[j] = tc[j - 1] + cost[j]
for i in range(1, m + 1):
for j in range(1, n + 1):
tc[i][j] = max(tc[i - 1][j - 1], tc[i - 1][j], tc[i][j - 1]) + cost[i][j]
return tc[m][n]