http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3631
1 #include <iostream> 2 #include <cmath> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 int arr[50]; 7 int n,mx,m; 8 void dfs(int cur,int total) 9 { 10 if(mx==m) return ; 11 if(total>mx&&total<=m) mx=total; 12 for(int i=cur+1;i<n;i++) if(total+arr[i]<=m) 13 { 14 dfs(i,total+arr[i]); 15 } 16 } 17 int main() 18 { 19 int sum; 20 while(cin>>n>>m) 21 { 22 mx=0;sum=0; 23 for(int i=0;i<n;i++) {scanf("%d",arr+i);sum+=arr[i];} 24 if(sum<=m) {cout<<sum<<endl;continue;} 25 for(int i=0;i<n;i++) dfs(i,arr[i]); 26 cout<<mx<<endl; 27 } 28 return 0; 29 }