xfstart07
Get busy living or get busy dying

#include < iostream >
using   namespace  std;

int  v,n;
int  f[ 100100 ] = { 0 };
int  p[ 100100 ];
int  a[ 110 ];
int  main()
{
    scanf(
" %d%d " , & v, & n);
    
int  sum = 0 ;
    
for ( int  i = 1 ;i <= n; ++ i){
        scanf(
" %d " , & a[i]);
        sum
+= a[i];
    }
    sum
-= v;
    
if (sum < 0 ){
        printf(
" 0\n " );
        
return   0 ;
    }
    f[
0 ] = 1 ;
    
for ( int  i = 1 ;i <= n; ++ i)
        
for ( int  j = sum - a[i];j >= 0 ; -- j)
            
if (f[j]){
                
if ( ! f[j + a[i]])
                    p[j
+ a[i]] = i;
                f[j
+ a[i]] += f[j];
            }
    
if (f[sum] == 0 ) printf( " 0\n " );
    
else   if (f[sum] > 1 ) printf( " -1\n " );
    
else {
        
int  l = 0 ,ans[ 110 ];
        
for ( int  i = n;i >= 1 ; -- i)
            
if (p[sum] == i)
            {
                ans[
++ l] = i;
                sum
-= a[i];
            }
        
for ( int  i = l;i >= 1 ; -- i)
            printf(
" %d  " ,ans[i]);
    }
    
return   0 ;
}



posted on 2009-06-04 17:49 xfstart07 阅读(222) 评论(0)  编辑 收藏 引用 所属分类: 代码库

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理