function solution(wlist, vlist, sumW) { let L = wlist.length, V = new Array(L) for (let i = 0; i < L; i++) { V[i] = new Array(sumW).fill(0) } for (let i = 0; i < L; i++) { for (let j = 1; j <= sumW; j++) { if (i > 0) { if (j < wlist[i]) { V[i][j] = V[i - 1][j] } else { V[i][j] = Math.max(V[i - 1][j - wlist[i]] + vlist[i], V[i - 1][j]) } } else { if (j >= wlist[i]) { V[i][j] = vlist[i] } } } } return V[L - 1][sumW] } let res = solution([3, 2, 5, 3], [2, 3, 4, 1], 12) console.log(res)