const stoneGameII = function (p) {
const pl = p.length;
const sums = new Array(pl + 1).fill(0);
const hash = new Array(pl).fill(0).map(() => new Array(pl).fill(0));
for (let i = pl - 1; i >= 0; i--) {
sums[i] = sums[i + 1] + p[i];
}
const hh = function (i, m) {
if (pl === i) return 0;
if (2 * m >= pl - i) return sums[i]; // 如果已经可以拿走所有的石头, 则直接返回剩下石头的总和
if (hash[i][m] !== 0) return hash[i][m];
let min = Number.MAX_SAFE_INTEGER;
for (let x = 1; x <= 2 * m; x++) {
min = Math.min(min, hh(i + x, Math.max(m, x))); // 下一个玩家会得到的最小分数
}
hash[i][m] = sums[i] - min;
return hash[i][m];
}
return hh(0, 1);
}