본문 바로가기

FrontEnd/PS through Javascript

백준(BOJ) 2798: 블랙잭

https://www.acmicpc.net/problem/2798

 

const fs=require('fs');
const filepath=process.platform==='linux'?'dev/stdin':'./input.txt';
let input=fs.readFileSync(filepath).toString().split("\n");
input[0]=input[0].split(" ").map(ele=>+(ele));
const n=input[0][0];
const target=input[0][1];
const arr=input[1].split(" ").map(ele=>+(ele));

let elements=new Array(3);
// console.log(elements);
console.log(solution(0, elements, 0));

function solution(depth, elements, start){
    if(depth==3){
        let result=0;
        for(let i=0;i<3;++i){
            result+=elements[i];
        }
        return result;
    }
    let answer=0;
    for(let i=start;i<arr.length;++i){
        elements[depth]=arr[i];
        let result=solution(depth+1, elements, i+1);
        if(result<=target &&result>answer)answer=result;
    }
    return answer;
}