编辑代码

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
  const n = Number(await readline());
  const scores = (await readline()).split(" ").map(Number);
  const k = Number(await readline());
  const dp = new Array(n).fill(0);
  dp[0] = scores[0];
  // 状态转移方程
  for (let i = 1; i < n; i++) {
    let max = -Infinity;
    // 考虑上k个前置元素
    for (let j = i - 1; j >= Math.max(i - k, 0); j--) {
      max = Math.max(max, dp[j]);
      // max = Math.max(max, dp[j]);
      // console.log(max);
    }
    dp[i] = max + scores[i];
  }
  // console.log(dp);
  console.log(dp[n - 1]);
})();