https://www.acmicpc.net/problem/1049
1049번: 기타줄
첫째 줄에 N과 M이 주어진다. N은 100보다 작거나 같은 자연수이고, M은 50보다 작거나 같은 자연수이다. 둘째 줄부터 M개의 줄에는 각 브랜드의 패키지 가격과 낱개의 가격이 공백으로 구분하여 주
www.acmicpc.net
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");
const [N, M] = input.shift().split(" ").map( i => Number(i));
const packages = [];
const singles = [];
let cost = 0;
input.forEach((i) => {
const [p, s] = i.split(" ").map( m => Number(m));
packages.push(p);
singles.push(s);
});
let packagePay = Math.floor(N/6);
// packages 가격 중 최솟값이 singles 최솟값 * 6 보다 큰 경우 낱개로 사는게 이득
cost += Math.min(...packages) > Math.min(...singles) * 6 ? Math.min(...singles) * packagePay * 6 : Math.min(...packages) * packagePay;
// 낱개 최솟값 * 남은 구매 수 보다 패키지 최솟값으로 구매하는 것이 더 싼 경우 패키지 구매가 이득
cost += Math.min(...singles) * (N - (packagePay) * 6) < Math.min(...packages) ? Math.min(...singles) * (N - (packagePay) * 6) : Math.min(...packages)
console.log(cost);
728x90
'Algorithm&CodingTest > Baekjoon' 카테고리의 다른 글
[ Baekjoon ] [11724] DFS/BFS 실버 2 - 연결 요소의 개수 (0) | 2023.03.14 |
---|---|
[ Baekjoon ] [1260] DFS/BFS 실버 2 - DFS와 BFS (0) | 2023.03.13 |
[ Baekjoon ] [2607] 실버 3 - 비슷한 단어 (1) | 2023.03.12 |
[ Baekjoon ] [13305] 실버 3 - 주유소 (0) | 2023.03.12 |
[Baekjoon] [10814] 나이순 정렬 (0) | 2023.02.24 |