dfs
#include <iostream>
#include <algorithm>
using namespace std;
int ar[25],n,b,ans=0x3f3f3f3f;
void dfs(int i,int high)
{
int j;
if (high>=b)
{
if (high<ans)ans=high;
return;
}
for (j=i;j<n;j++)
{
dfs (j+1,ar[j]+high);
}
}
int main()
{
int i,j;
cin>>n>>b;
for (i=0;i<n;i++)
{
cin>>ar[i];
}
sort(ar,ar+n);
dfs(0,0);
cout<<ans-b<<endl;
return 0;
}
dp
#include <iostream>
#include <algorithm>
using namespace std;
int dp[1000005]={0};
int main()
{
int i,j,n,b,ar[25],ans=0x3f3f3f3f,sum=0;
cin>>n>>b;
for (i=0;i<n;i++)
{
cin>>ar[i];
sum+=ar[i];
}
for (i=0;i<n;i++)
{
for (j=sum;j>=ar[i];j--)
{
dp[j]=max(dp[j],dp[j-ar[i]]+ar[i]);
}
}
for (i=b;i<=sum;i++)
{
if(dp[i]>=b)
ans=min(ans,dp[i]);
}
cout<<ans-b<<endl;
return 0;
}