AcWing 1016. 最大上升子序列和(Javascript)
原题链接
简单
作者:
cp777
,
2021-03-10 13:11:12
,
所有人可见
,
阅读 418
const N = 5010;
let q = [];
let f = new Int32Array(N);
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 === 1) {
let res = 0;
let arr = getInputNums(line);
for (let i = 1; i <= arr.length; i++) f[i] = q[i] = arr[i - 1];
for (let i = 1; i <= n; i++) {
for (let j = 1; j < i; j++) {
if (q[i] > q[j]) {
f[i] = Math.max(f[i], f[j] + q[i]);
}
}
res = Math.max(res, f[i]);
}
console.log(res);
}
});
});