#include < bits/stdc++.h >
using namespace std;
bool**dp;
void printvector(vector < int > &v)
{
for(int i=0;i < v.size();i++)
cout << v[i] << " ";
cout << endl;
}
void print(int a[], int i,int sum, vector < int > &v)
{
if(i==1 && sum!=0 && dp[1][sum])
{
v.push_back(a[i-1]);
if(a[i-1]==sum)
printvector(v);
return;
}
if(i==1 && sum==0)
{
printvector(v);
return;
}
//excluding given element make a new path
if(dp[i-1][sum])
{
vector < int > p=v;
print(a,i-1,sum,p);
}
//including given element
if (sum >= a[i-1] && dp[i-1][sum-a[i-1]])
{
v.push_back(a[i-1]);
print(a, i-1, sum-a[i-1],v);
}
}
int main()
{
int n,sum;
cin >> n >> sum;
int a[n];
for(int i=0;i < n;i++)
cin >> a[i];
dp = new bool*[n+1];
for(int i=0;i <=n;i++)
dp[i]=new bool[sum+1];
//initialize if i==0 dp[i][j]=False if j==0 dp[i][j]=true but if i==0 && j==0 dp[i][j]=True
dp[0][0]= 1;
for(int i=1;i <=n;i++)
dp[i][0]=1;
for(int j=1;j <=sum;j++)
dp[0][j]=0;
for(int i=1;i <=n;i++)
{
for(int j=1;j <=sum;j++)
{ //exclude a[i] for sum j
if(a[i-1] > j)
dp[i][j]=dp[i-1][j];
//else include if j-a[i] is true
else
dp[i][j]=dp[i-1][j-a[i-1]] || dp[i-1][j];
}
}
if(dp[n][sum])
cout << "YES" << endl;
else
cout << "NO" << endl;
vector < int > v;
print(a,n,sum,v);
return 0;
}
5 10
1 2 3 4 5
0 Comments