/************************************************************************\
n个物品,物品i 重量wi>0 价值为vi 背包最大能W goal->求不超过容量的最大价值
Item Value Weight
1 1 1
2 6 2
3 18 5
4 22 6
5 28 7 (W = 11)
Def. OPT(i, W) = max profit subset of items 1, …, i with weight limit W.
Case 1: OPT does not select item i.
OPT selects best of { 1, 2, …, i-1 } using weight limit W
Case 2: OPT selects item i.
new weight limit = W – wi
OPT selects best of { 1, 2, …, i–1 } using this new weight limit
\************************************************************************/
#include<iostream>
#include<fstream>
using namespace std;
#define max(a,b)(a>b ? a:b)
const int MAX = 100;
int Weight; //max weight of the pack
int w[MAX];
int v[MAX];
int dp[MAX];//dp[i] imply that the max value under the weight of i
//滚动数组 由于dp[i][j]只与dp[i-1][X]有关系 所以可以减少到一维数组
int solveArray(int n)
{
int i,j;
for(i=0; i<=Weight; i++)//init dp
dp[i] = 0;
for(i=1; i<=n; i++)
{
for(j=Weight; j>=w[i]; j--)
{
//if select the ith goods,then dp[j] = v[i]+dp[j-w[i]]
dp[j] = max(dp[j], v[i] + dp[j-w[i]]);
}
}
return dp[Weight];//return max value
}
int main()
{
ifstream cin("in.txt");
//ofstream cout("out.txt");
int i, n;
while(cin >> n)
{
if(n==0) break;
cin >> Weight;
for(i=1; i<=n; i++)
cin >> v[i];
for(i=1; i<=n; i++)
cin >> w[i];
cout << "max value :" << endl;
int ans = solveArray(n);
cout << ans << endl;
}
return 0;
}