/**
 *
 * @param {Array} weights  每件物品的重量
 * @param {Array} values  每件物品的价值
 * @param {Number} capacity  背包的总容量
 */
function knapsack(weights, values, W) {
    var n = weights.length;
    var dp = [[], []],cur;
    for (var j = 1; j <= W; j++) {
        dp[0][j] = 0;
        for (var i = 0; i <= n; i++) {
            if (i % 2) {
                var cur = dp[i];
                if (j < weights[i]) {
                    dp[1][j] = dp[0][j];
                } else {
                    dp[1][j] = Math.max(dp[0][j - weights[i]] + values[i], dp[0][j]);
                }
            } else {
                var cur = dp[i];
                if (j < weights[i]) {
                    dp[0][j] = dp[1][j];
                } else {
                    dp[0][j] = Math.max(dp[0][j - weights[1]] + values[i], dp[1][j]);
                }
            }
        }
    }
    return matrix[n - 1][capacity - 1]; //在目前背包情况下能装下的最大价值
}
var a = knapsack([4, 3, 5, 2, 5], [9, 6, 1, 4, 1], 10);
console.log(a);
var values = [6, 5, 10, 11, 13]; //价值
var size = [3, 4, 7, 8, 9]; //尺寸
var capacity = 16; //背包容量
console.log(knapsack(size, values, capacity));
