编辑代码

function optimizeTravel(days, time, score) {
  const n = time.length
  const dp = new Array(n + 1).fill(0).map(() => new Array(days + 1).fill(0))

  for (let i = 1; i <= n; i++) {
    for (let j = 1; j <= days; j++) {
      if (time[i - 1] > j) {
        dp[i][j] = dp[i - 1][j]
      } else {
        dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - time[i - 1]] + score[i - 1])
      }
    }
  }

  return dp[n][days]
}

const days = 4
const time = [1, 2, 3, 1]
const score = [7, 8, 9, 6]

console.log(optimizeTravel(days, time, score)) // 输出 16