-
Notifications
You must be signed in to change notification settings - Fork 13
/
dp-4(howsum).cpp
50 lines (49 loc) · 943 Bytes
/
dp-4(howsum).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
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int check[10000];
vector<int>v;
bool howsum(int n,int m,int a[])
{
if(n==0)
{
cout<<"hurrah"<<endl;
return true;
}
if(n<0)return false;
if(check[n]==1)return true;
if(check[n]==-1)return false;
for(int i=0; i<m; i++)
{
int r=n-a[i];
if(howsum(r,m,a)==true)
{
v.push_back(a[i]);
check[n]=1;
return true;
}
else
{
check[n]=-1;
}
}
return false;
}
int main()
{
int n,m;
cin>>n>>m;
int a[m];
for(int i=0; i<m; i++)
{
cin>>a[i];
}
if(howsum(n,m,a)==true){
cout<<"yes"<<endl;
for(int i=0;i<v.size();i++)cout<<v[i]<<" ";
cout<<endl;
}
else
cout<<"no ans"<<endl;
return 0;
}