显示代码纯文本
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
using namespace std;
int read()
{
int x,f=1;
char ch;
while(ch=getchar(),!isdigit(ch))if(ch=='-')f=-1;
x=ch-48;
while(ch=getchar(),isdigit(ch))x=x*10+ch-48;
return x*f;
}
int f[101000],an[150],a[150],flag[101000];
int main()
{
freopen("bagb.in","r",stdin);
freopen("bagb.out","w",stdout);
int now=read(),n=read(),tot=0;
for(int i=1;i<=n;i++)
{
a[i]=read();
tot+=a[i];
}
tot-=now;
f[0]=1;
for(int i=1;i<=n;i++)
{
for(int j=tot;j>=a[i];j--)
{
if(f[j-a[i]])
{
if(!f[j])f[j]=i;
flag[j]++;//,cout<<j<<' '<<flag[j]<<endl;
}
}
}
int cnt=0;
if(!f[tot]){puts("0");/*while(1);*/return 0;}
if(flag[tot]>1){/*cout<<i<<endl;*/puts("-1");/*while(1);*/return 0;}
for(int i=tot;i>0;)
{
an[++cnt]=f[i],i=i-a[f[i]];//cout<<i<<' '<<f[i]<<endl;
}
sort(an+1,an+1+cnt);
for(int i=1;i<=cnt;i++)printf("%d ",an[i]);//while(1);
// system("pause");
}