๐Ÿ’ก๋ฌธ์ œ ๋ถ„์„ ์š”์•ฝ

๐Ÿ’ก์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๊ณ„

๐Ÿ’ก์ฝ”๋“œ

const fs = require('fs');
const path = require('path');

const input = fs.readFileSync(path.join(__dirname, 'input.txt')).toString().trim().split('\\n');

const [n, S] = input[0].split(' ').map(Number);
const arr = input[1].split(' ').map(Number);

let count = 0;

function dfs(index, sum) {
  if (index === n) return;
  
  sum += arr[index];

  // ๋ถ€๋ถ„์ˆ˜์—ด์˜ ํ•ฉ์ด S์™€ ๊ฐ™์€์ง€ ํ™•์ธ
  if (sum === S) count++;
  
  // ํ˜„์žฌ ์›์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ์™€ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๋ชจ๋‘ ํƒ์ƒ‰
  dfs(index + 1, sum);  // ํ˜„์žฌ ์›์†Œ๋ฅผ ํฌํ•จํ•œ ๊ฒฝ์šฐ
  dfs(index + 1, sum - arr[index]);  // ํ˜„์žฌ ์›์†Œ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
}

// ํƒ์ƒ‰ ์‹œ์ž‘
dfs(0, 0);

console.log(count);

๐Ÿ’ก์‹œ๊ฐ„๋ณต์žก๋„

image.png