function completeKnapsack(weights, values, W) {
    var dp = [],
        n = weights.length;
    dp[-1] = []; //初始化边界
    for (let i = 0; i <= W; i++) {
        dp[-1][i] = 0;
    }
    for (let i = 0; i < n; i++) {
        dp[i] = [];
        for (let j = 0; j <= W; j++) {
            if (j < weights[i]) {
                dp[i][j] = dp[i - 1][j];
            } else {
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - weights[i]] + values[i]);
            }
        }
        console.log(dp[i].concat());
    }
    return dp[n - 1][W];
}

var a = completeKnapsack([3, 2, 2], [5, 10, 20], 5); //输出40
console.log(a);
var b = completeKnapsack([2, 3, 4, 7], [1, 3, 5, 9], 10); //输出12
console.log(b);
