Recursion ( can someone let me know why this is wrong, please)


#1
int health(vector<vector<int>>&A, int i, int j, int n, int m, vector<vector<int>>&dp){
    if(i>=n || j>=m) return INT_MIN;
    if(i==n-1 && j==m-1) return A[i][j];
    if(dp[i][j]!=-1) return dp[i][j];
    return dp[i][j]=A[i][j]+max(health(A,i+1,j,n,m,dp),health(A,i,j+1,n,m,dp));
}
int Solution::calculateMinimumHP(vector<vector<int> > &A) {
    vector<vector<int>> dp(A.size(),vector<int>(A[0].size(),-1));
    int ans = health(A,0,0,A.size(),A[0].size(),dp);
    if(ans<0) return 1+abs(ans);
    return 1;
}

Here, i am using max because if i get +ve number at the end i will return 1, if i get -ve number i will choose max -ve number which will give me least health at the end.