提交时间:2024-04-06 15:16:14
运行 ID: 141965
#include<bits/stdc++.h> using namespace std; int s,n,w[35]; bool used[35]; bool a(int m) { if(m==0)return 1; else { for(int i=n;i>=1;i--) { if(w[i]<=m&&used[i]==0) { used[i]=1; if(a(m-w[i]))return 1; used[i]=0; } } return 0; } } int main() { cin>>s>>n; for(int i=1;i<=n;i++)cin>>w[i]; sort(w+1,w+1+n); if(a(s)) { for(int i=1;i<=n;i++) if(used[i])cout<<i<<endl; } else cout<<"Failed!"; return 0; }