题目传送门
解题思路:
01背包.
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<vector> 4 5 using namespace std; 6 7 int n,sum[101],id,_min = 1999999999; 8 vector<int> a[101]; 9 bool f[101][10001]; 10 11 int main() { 12 scanf("%d",&n); 13 for(int i = 1;i <= n; i++) { 14 while(true) { 15 int u; 16 scanf("%d",&u); 17 if(u == -1) break; 18 a[i].push_back(u); 19 sum[i] += u; 20 } 21 if(_min > sum[i]) { 22 _min = sum[i]; 23 id = i; 24 } 25 f[i][0] = 1; 26 } 27 for(int i = 1;i <= n; i++) 28 for(int j = 0;j < a[i].size(); j++) 29 for(int k = sum[i];k >= a[i][j]; k--) 30 f[i][k] = f[i][k] | f[i][k - a[i][j]]; 31 _min++; 32 while(_min--) { 33 bool flag = 1; 34 for(int j = 1;j <= n; j++) 35 if(!f[j][_min]) 36 flag = 0; 37 if(flag) { 38 printf("%d",_min); 39 return 0; 40 } 41 } 42 printf("0"); 43 return 0; 44 }