-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind More Coins.cpp
62 lines (60 loc) · 1.11 KB
/
Find More Coins.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 <algorithm>
#include <iostream>
#include <vector>
std::vector<int> ans;
int n, m, temp[10010], a[10010];
int dfs(int v, int deep, int sum)
{
temp[deep] = a[v];
sum += a[v];
deep++;
if (sum == m)
{
for (int i = 0; i < deep; i++)
{
ans.push_back(temp[i]);
}
return 0;
}
for (int i = v + 1; i < n; i++)
{
if (sum + a[i] > m)
break;
dfs(i, deep, sum);
if (ans.size() != 0)
break;
}
return 0;
}
int main()
{
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++)
{
int temp;
scanf("%d", &temp);
a[i] = temp;
}
std::sort(a, a + n);
ans.clear();
for (int i = 0; i < n; i++)
{
if (a[i] > m)
break;
dfs(i, 0, 0);
if (ans.size() != 0)
break;
}
if (ans.size() == 0)
printf("No Solution");
else
{
for (int i = 0; i < ans.size(); i++)
{
printf("%d", ans[i]);
if (i != ans.size() - 1)
printf(" ");
}
}
return 0;
}