Fill the dp array
for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( ( A [ i - 1 ] <= j ) && ( A [ i - 1 ] != 0 ) ) dp [ i , j ] = dp [ i - 1 , j ] + dp [ i - 1 , j - A [ i - 1 ] ] ; else dp [ i , j ] = dp [ i - 1 , j ] ; } }