๐งธ ์ ์ "ํด์ฌ" ๋ฌธ์ ๋ฅผ ํ์๋๋ฐ ์ค์ค๋ก ๋ชปํ์์ด์ ํด์ฌ2์ ๋์ ํด๋ดค๋ค.
๐งธ ๋ด๊ฐ ์ค์ค๋ก ์ด ์ฝ๋๋ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ค. "ํด์ฌ"์ "ํด์ฌ2"์ ์ฐจ์ด๋ N์ ๋ฒ์์๋ค. ๋ด ์ฝ๋๋ "ํด์ฌ"๋ฌธ์ ๋ ํ ์ ์์์ง๋ง "ํด์ฌ2"๋ ํ์ง๋ชปํ๋ค. ์ ์ ํผ์ ๋ชปํ๋ "ํด์ฌ"๋ฌธ์ ๋ฅผ ์ค์ค๋ก ํผ๊ฑด ์ข์์ง๋ง "ํด์ฌ2"๋ ํด๋ด์ง ๋ชปํ๊ฒ์ด ์์ฝ๋ค.
๐ก max๋ฅผ ๋ฐ๋ก ์ ์ฅํ๋ฉด์ ํ๋ฉด ๋ฐ๋ณต๋ฌธ์ ํ ๋ฒ ๋ ์ฌ์ฉํ์ง ์์๋ ํ ์ ์๋ค.
๐งธ ๋ฐ๋ก ์ ์ฅํ๋ฉด ๋๋ค๋ ์๊ฐ์ ํ๋๋ฐ ๋ด ๋จธ๋ฆฟ์์์๋ ์๋๋ ๊ฒ ๊ฐ์๋ฐ..?๋ก ๋๋ฌ๋ค. ์กฐ๊ธ ๋ ์ฐจ๋ถํ, ์นจ์ฐฉํ๊ฒ ์๊ฐํ์ผ๋ฉด ํ ์ ์์ง ์์์๊น ์๊ฐํ๋ค.
์ ๋ต ์ฝ๋
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const N = +input.shift();
const dp = new Array(N + 1).fill(0);
const schedule = input.map((item) => item.split(" ").map(Number));
let max = 0;
for (let i = 0; i < N; i++) {
const [day, cost] = schedule[i];
max = Math.max(dp[i], max);
if (i + day <= N) dp[i + day] = Math.max(dp[i + day], max + cost);
}
console.log(Math.max(...dp));
์๊ฐ์ด๊ณผ ์ฝ๋
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const N = +input.shift();
const dp = new Array(N).fill(0);
const schedule = input.map((item) => item.split(" ").map(Number));
for (let i = 0; i < N; i++) {
const [day, cost] = schedule[i];
if (i + day - 1 < N) {
dp[i] += cost;
if (i + day < N) {
for (let j = i + day; j < N; j++) {
dp[j] = Math.max(dp[i], dp[j]);
}
}
}
}
console.log(Math.max(...dp));