Problem Description
在一批共n个集装箱要装上艘载重量为c的轮船,其中集装箱i的重量为wi。找出一种最优装载方案,将轮船尽可能装满,即在装载体积不受限制的情况下,将尽可能重的集装箱装上轮船。
Input
输入有多组数据,每组数据的第一行有两个正整数n和c。n是集装箱数,c是轮船的载重量。接下来的1行中有n个正整数,表示集装箱的重量。
Output
对于每组数据输出最大装载重量。
Sample Input
5 10 7 2 6 5 4
Sample Output
10
<pre name="code" class="cpp">#include <cstdio> #include <cstring> #include <iostream> using namespace std; int c , n , ans; int a[1000]; bool f[10000]; int main() { while (cin >> n >> c) { for(int i = 1 ; i <= n ; i++) scanf("%d" , &a[i]); ans = 0; memset(f,0,sizeof(f)); f[0] = true; for(int i = 1 ; i <= n ; i++) for(int j = c ; j >= a[i] ; j--) if (f[j - a[i]] == true) { f[j] = true; if (j > ans) ans = j; } cout << ans << endl; } return 0; }