# 背包问题-多重背包问题

public class Main {
static int V = 5;//总体积为5
static int n = 4;//物品数量为4
static int[] v = {0, 1, 2, 3, 4};//每个物品的体积为vi
static int[] w = {0, 2, 4, 4, 5};//每个物品的价值为wi
static int[] s = {0, 3, 1, 3, 2};// 每个物品最多s件

public static void main(String[] args) {
int f[] = new int[V + 1];
for (int i = 1; i <= n; i++)
for (int j = V; j >= v[i]; j--)
for (int k = 0; k * v[i] <= j && k <= s[i]; k++)
f[j] = Math.max(f[j], f[j - k * v[i]] + k * w[i]);
System.out.println(f[V]);
}
}


public class Main1 {
static int V = 5;
static int n = 4;
static int[] v = {0, 1, 2, 3, 4};
static int[] w = {0, 2, 4, 4, 5};
static int[] s = {0, 3, 1, 3, 2};// 每个物品最多s件
static class Good{
int v,w;
public Good(int v, int w) {
this.v = v;
this.w = w;
}
}

public static void main(String[] args) {
for(int i = 1;i < n;i++){
for(int k =1;k<= s[i];k*=2){
s[i] -= k;
goods.push(new Good(v[i]*k,w[i]*k));
}
//除不尽时
if(s[i]>0) goods.push(new Good(v[i]*s[i],w[i]*s[i]));
}
int [] f = new int[V+1];
for(Good g : goods)
for(int j = V;j>= g.v;j--)
f[j] = Math.max(f[j],f[j-g.v]+g.w);
System.out.println(f[V]);
}
}