1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 #include <cstdio> 5 #include <cstring> 6 using namespace std; 7 const int N=1e4+7; 8 int a[N]; 9 bool dp[107]; 10 vector < vector <int> > ans (107); 11 int n,m; 12 bool cmp (vector<int> x, vector<int> y) {// 不用考虑x与y得大小 因为至少一个数不同 或者全部相同 13 for (int i=0;i<x.size();i++) { 14 if (x[i]<y[i]) return 1; 15 else if (x[i]>y[i]) return 0; 16 } 17 return 0; 18 } 19 int main () 20 { 21 scanf ("%d %d",&n,&m); 22 for (int i=1;i<=n;i++) 23 scanf ("%d",&a[i]); 24 sort (a+1,a+1+n); 25 dp[0]=1; 26 for (int i=1;i<=n;i++) { 27 for (int j=m;j>=a[i];j--) { 28 if (dp[j-a[i]]) { 29 vector <int> tmp=ans[j-a[i]]; 30 tmp.push_back(i); 31 if (!dp[j]) { dp[j]=1;ans[j]=tmp; } 32 else if (cmp (tmp,ans[j])) ans[j]=tmp; 33 } 34 } 35 } 36 if (!dp[m]) printf ("No Solution\n"); 37 else { 38 printf ("%d",a[ans[m][0]]); 39 for (int i=1;i<ans[m].size();i++) 40 printf (" %d",a[ans[m][i]]); 41 printf ("\n"); 42 } 43 return 0; 44 }
抓住青春的尾巴。。。