AcWing 898. 数字三角形. (JavaScript )
原题链接
简单
作者:
cp777
,
2021-03-05 00:11:02
,
所有人可见
,
阅读 299
const N = 510;
const INF = 1e9;
let tri = [];
let f = [];
for (let i = 0; i < N; i++) {
tri[i] = new Int32Array(N);
f[i] = new Int32Array(N).fill(-INF);
}
let n = 0;
let cnt = 0;
let buf = '';
process.stdin.on('readable', function () {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
let getInputNums = line => line.split(' ').filter(s => s !== '').map(x => parseInt(x));
let getInputStr = line => line.split(' ').filter(s => s !== '');
process.stdin.on('end', function () {
buf.split('\n').forEach(function (line, lineIdx) {
if (lineIdx === 0) {
n = getInputNums(line)[0];
} else if (lineIdx <= n) {
cnt++;
let arr = getInputNums(line);
for (let i = 1; i <= cnt; i++) {
tri[cnt][i] = arr[i - 1];
}
if (lineIdx === n) {
f[1][1] = tri[1][1];
for (let i = 2; i <= n; i++) {
for (let j = 1; j <= i; j++) {
f[i][j] = Math.max(f[i - 1][j - 1], f[i - 1][j]) + tri[i][j];
}
}
let res = -INF;
for (let i = 1; i <= n; i++) {
res = Math.max(res, f[n][i]);
}
console.log(res);
}
}
});
});