题目地址:http://ac.jobdu.com/problem.php?cid=1040&pid=23
C语言源码:
#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
return *(int *)a>*(int *)b?-1:1;
}
int main()
{
int i,n,a[601],L,len;
while(scanf("%d %d",&L,&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(a,n,sizeof(a[0]),cmp);
len=0;
i=0;
while(i<n&&len<L)
len+=a[i++];
if(len>=L)
printf("%d\n",i);
else
printf("impossible\n");
}
}