๐งธ ์ ํผ์์๋ ์ํ๋ฆฌ๋์ง ๋ต์ ๋ณด๋ฉด ์ถฉ๋ถํ ํ ์ ์์๋ ๋ฌธ์ ์๋ ๊ฒ ๊ฐ์๋ฐ ๋๋ฌด ๋ณต์กํ๊ฒ ์๊ฐํ๋ ๊ฒ ๊ฐ๊ธฐ๋ํ๋ค.. ๋ค๋ฅธ ์์ฉ๋ฌธ์ ๋ฅผ ์กฐ๊ธ ๋ ํ์ด๋ด์ผ๊ฒ ๋ค.
๐จ ์ฐธ๊ณ ์ฝ๋
์ฝ๋
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");
const N = +input.shift();
const eggs = input.map((el) => el.split(" ").map(Number));
const eggsState = eggs.map((el) => el[0]);
let answer = 0;
const dfs = (index, arr) => {
if (index === N) {
const count = arr.filter((el) => el <= 0).length;
if (answer < count) answer = count;
return;
}
//์์ ๋ ๊ณ๋์ด ๊นจ์ง ์ํ
if (arr[index] <= 0) {
dfs(index + 1, arr);
return;
}
//๊นจ์ง์ง ์์ ๊ณ๋ ์น๊ธฐ
let pass = true;
for (let i = 0; i < N; i++) {
if (i !== index && arr[i] > 0) {
pass = false;
const temp = [...arr];
temp[i] -= eggs[index][1];
temp[index] -= eggs[i][1];
dfs(index + 1, temp);
}
}
//์น ๊ณ๋์ด ์๋ ๊ฒฝ์ฐ
if (pass) {
if (answer < N - 1) {
answer = N - 1;
}
return;
}
};
dfs(0, eggsState);
console.log(answer);