Run Time Error (Able to print the final answer)


#1

int Solution::solve(vector &A){
int sum = 0;
for(int i = 0; i<A.size();i++)
sum+=A[i];

vector<vector<int>> dp (A.size()+1,vector<int> (sum/2 + 1,0));
for(int i = 0; i<=A.size(); i++)
    dp[i][0] = 1;

for(int i = 1; i<=A.size(); i++)
    for(int j = 1; j<=sum/2 + 1; j++)
    {
        if(A[i-1]>j) dp[i][j] = dp[i-1][j];
        else 
        {
            dp[i][j] = dp[i-1][j-A[i-1]];
        }
    }

int ans = INT_MAX;
for(int i = sum/2 ; i>= 0; i--)
{
    if(dp[A.size()][i]==1)
    {
        ans = sum - 2*i;
        break;
    }
}

return ans;

}