forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubset_sum_problem.cpp
63 lines (61 loc) · 1.3 KB
/
subset_sum_problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include<algorithm>
#include<cstring>
//#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define nt _int128
#define inf (int)1e9
int dp[101][50001]; // dp[n][sum]
bool istrue(ll arr[], int n, int sum)
{
if ((sum == 0) || (n == 0 && sum == 0))
return true;
if (n == 0)
return false;
if (dp[n - 1][sum] != -1)
return dp[n - 1][sum];
bool p = false, q = false;
if (sum >= arr[n - 1])
p = istrue(arr, n - 1, sum - arr[n - 1]);
q = istrue(arr, n - 1, sum);
dp[n - 1][sum] = p || q;
return dp[n - 1][sum];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
// t = 1;
while (t--)
{
int n;
cin >> n;
ll arr[n];
ll sum = 0;
memset(dp, -1, sizeof(dp));
for (ll i = 0; i < n; i++)
{
cin >> arr[i];
sum += arr[i];
}
if (sum % 2 != 0)
{
cout << "NO" << endl;
}
else
{
sum = sum / 2;
// cout << sum << endl;
if (istrue(arr, n, sum) == 0)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
return 0;
}