链接:点击打开链接
题意:给出n个数,在n个数中选取几个数加和,求大于k的最小和是多少
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include <climits>
using namespace std;
long long N,B,ans;
long long s[50];
void dfs(long long num,long long sum){
if(num==N+1)
return;
if(sum>=B){
if(sum<=ans)
ans=sum;
}
dfs(num+1,sum);
dfs(num+1,sum+s[num]);
} //dfs搜索也可以用01背包做
int main(){
long long i,j;
while(scanf("%lld%lld",&N,&B)!=EOF){
for(i=0;i<N;i++)
scanf("%lld",&s[i]);
ans=INT_MAX;
dfs(0,0);
printf("%lld\n",ans-B);
}
return 0;
}