개발자 톡

연습문제 톡 금고털이

2, 4, 5 케이스의 반례를 알 수 있을까요?

등록일
2023-12-01 22:05:29
조회수
457
작성자
doch2130
const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

function solution() {
  const input = [];
  rl.on('line', line => {
    input.push(line.trim());
  }).on('close', () => {
    const [bag, kind] = input.shift().split(' ').map(Number);


    const jewelWeight = [];
    const jewelValue = [];
    
    input.forEach((line, index) => {
      const [weight, value] = line.split(' ').map(Number);
      jewelWeight.push(weight);
      jewelValue.push(value);
    });
    
    let sum = 0;
    let isCheckWeight = bag;
    
    while(isCheckWeight > 0) {
      let maxValueIndex = jewelValue.indexOf(Math.max(...jewelValue));

      if(Math.max(...jewelValue) === 0) {
        break;
      }

      if(jewelWeight[maxValueIndex] >= isCheckWeight) {
        sum += (isCheckWeight * jewelValue[maxValueIndex]);
        break;
      } else {
        sum += (jewelWeight[maxValueIndex] * jewelValue[maxValueIndex]);
        isCheckWeight -= jewelWeight[maxValueIndex];
        jewelValue[maxValueIndex] = 0;
      }
    }

    console.log(sum);
    
    return ;
    
  });
}

solution();


#금고털이

이 카테고리의 톡 더보기