function completeKnapsack(weights, values, W) {
  
      var  n = weights.length;
        dp = new Array(W+1).fill(0)
  
    for (let i = 0; i < n; i++) {
        if(weights[i]<j)
          dp[j] = Math.max(dp[j], dp[j-weights[i]]+values[i]);
       }
    }
    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);
