|
|
back to boardWhat's wrong with this code?(+) Posted by asif 6 Mar 2003 12:20 # include <stdio.h> # define N 100 # define M 100000 int m,n,w[N+1],c[M+1],p[M+1]; int sum,b[N+1],tot; int main(void) { int i,j; scanf("%d%d",&m,&n); for(i=1;i<=n;i++){ scanf("%d",&w[i]); sum+=w[i]; } m=sum-m; c[0]=1; for(i=1;i<=n;i++) for(j=m;j>=w[i];j--) if(c[j-w[i]]>0){ c[j]+=c[j-w[i]]; if(c[j]>1) c[j]=2; p[j]=i; } if(m<0 || c[m]==0) printf("0\n"); else if(c[m]>1) printf("-1\n"); else{ j=m; while(j>0){ i=p[j]; b[i]=1; tot++; j-=w[i]; } for(i=1;i<=n;i++) if(b[i]){ printf("%d%s",i,tot==1? "\n":" "); tot--; } } return 0; } |
|
|