
이전에 Python으로 풀었던 [Softeer] 금고털이 문제를 JavaScript로 다시 풀었다. 설명은 요기 위 링크에 !
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const jewelry = [];
let [W, N] = input[0].split(" ").map(Number);
for (let i = 1; i <= N; i++) {
const [M, P] = input[i].split(" ").map(Number);
jewelry.push([M, P]);
}
let result = 0;
jewelry
.sort((a, b) => b[1] - a[1]) // 무게당 가격이 높은 순으로 정렬
.forEach(([weight, price]) => {
if (weight <= W) {
result += weight * price;
W -= weight;
} else {
result += W * price;
W = 0;
}
});
console.log(result);
