<p>You are given an integer array <code>nums</code> and an integer <code>target</code>.</p>

<p>You want to build an <strong>expression</strong> out of nums by adding one of the symbols <code>&#39;+&#39;</code> and <code>&#39;-&#39;</code> before each integer in nums and then concatenate all the integers.</p>

<ul>
	<li>For example, if <code>nums = [2, 1]</code>, you can add a <code>&#39;+&#39;</code> before <code>2</code> and a <code>&#39;-&#39;</code> before <code>1</code> and concatenate them to build the expression <code>&quot;+2-1&quot;</code>.</li>
</ul>

<p>Return the number of different <strong>expressions</strong> that you can build, which evaluates to <code>target</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,1,1,1], target = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> There are 5 ways to assign symbols to make the sum of nums be target 3.
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1], target = 1
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 20</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
	<li><code>0 &lt;= sum(nums[i]) &lt;= 1000</code></li>
	<li><code>-1000 &lt;= target &lt;= 1000</code></li>
</ul>

class Solution {
    #define ll long long
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        ll n=nums.size(),i,j,sum=0,ans=0,no=0;
        if(n==1)
        {
            if(nums[0]==0)return 2;
            if(target==nums[0]||nums[0]==-target)return 1;
        }
        for(i=0;i<n;i++)
        {
            sum+=nums[i];
            if(nums[i]==0)no++;
        }
        vector<vector<ll>> dp(sum+1,vector<ll>(n+1,0));
        for(i=0;i<=sum;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(nums[j-1]==i)dp[i][j]=dp[i][j-1]+1+dp[0][j-1];
                else if(nums[j-1]<i)dp[i][j]=dp[i][j-1]+dp[i-nums[j-1]][j-1];
                else
                {
                    dp[i][j]=dp[i][j-1];
                }
                //cout<<dp[i][j]<<" ";
            }
            //cout<<endl;
        }
        for(i=0;i<=sum;i++)
        {
            ll s1=i,s2=sum-i;
            cout<<s1<<" "<<s2<<endl;
            if(s1-s2==target)
            {
                ans=dp[s1][n];
                //cout<<s1<<" ";
                break;
            }
        }
        if(sum==-target)return ans+1;
        if(no==n)return ans+1;
        return ans;
    }
};
30051