개발자 톡
연습문제 톡
금고털이
JS
- 등록일
- 2024-11-08 14:27:00
- 조회수
- 23
- 작성자
- vavoya6324
const fs = require('fs'); let [bagInfo, ...jewelInfos] = fs.readFileSync('input.txt', 'utf8').trim().split('\n'); jewelInfos = jewelInfos.map(jewelInfo => jewelInfo.split(' ').map(Number)).sort((a, b) => b[1] - a[1]); let weight = bagInfo.split(' ')[0] let result = 0 let jewelIndex = 0; while (weight > 0) { if (weight >= jewelInfos[jewelIndex][0]) { weight -= jewelInfos[jewelIndex][0] result += jewelInfos[jewelIndex][0] * jewelInfos[jewelIndex][1] jewelIndex++; } else { result += weight * jewelInfos[jewelIndex][1] weight = 0 } } console.log(result)
#금고털이