/**
 *
 * @param {Array} weights  每件物品的重量
 * @param {Array} values  每件物品的价值
 * @param {Number} capacity  背包的总容量
 */
function knapsack(weights, values, capacity) {
    //数组是当做指针传递的
    let n = weights.length; //物品的数量
    let matrix = []; //创建一个二维数组
    for (let i = 0; i < n; i++) {
        matrix[i] = [];
        for (let j = 0; j < capacity; j++) {
            matrix[i][j] = 0;
        }
    }
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < capacity; j++) {
            matrix[i][j] = i === 0 ? 0 : matrix[i - 1][j];
            if (i > 0 && j > weights[i - 1]) {
                matrix[i][j] = Math.max(matrix[i][j], matrix[i - 1][j - weights[i - 1]] + values[i - 1]);
            }
        }
    }
    return matrix[n-1][capacity-1];
}
var a = knapsack([4,3,5,2,5],[9,6,1,4,1], 10);