提交时间:2024-04-11 21:00:33

运行 ID: 143029

#include <bits/stdc++.h> using namespace std; int n, m; int a[40]; bool b2[40]; bool b = false; int maxn = 0; void dfs(int x, int sum) { if(b || sum>n) { return; } if(x==m+1) { if(sum==n) { for(int i = 1; i<=m; i++) { if(b2[i]) { cout << a[i] << endl; } } b=true; } return; } if(a[i]>=maxn) { int o = maxn; b2[x]=true; maxn=a[i]; dfs(x+1, sum+a[x]); maxn=o; b2[x]=false; } dfs(x+1, sum); } int main() { cin >> n >> m; for(int i = 1; i<=m; i++) { cin >> a[i]; } dfs(1, 0); if(b==false) { cout << "Failed!" << endl; } }