본문 바로가기
코딩테스트 연습/Greedy

[백준] 1049번 기타줄 (JavaScript)

by eddypark 2024. 5. 3.

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

const input = require('fs')
  .readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
  .toString()
  .trim()
  .split('\n')
  .map((el) => el.split(' ').map(Number));
// .map(Number);
let [N, M] = input.shift();

let sort_input = input.sort((a, b) => a[0] - b[0]);
let minSet = sort_input[0][0];
sort_input = sort_input.sort((a, b) => a[1] - b[1]);
let minPiece = sort_input[0][1];

let onlySet = Math.ceil(N / 6) * minSet;
let onlyPiece = N * minPiece;
let mix = Math.floor(N / 6) * minSet + (N % 6) * minPiece;
console.log(Math.min(onlySet, onlyPiece, mix));

구매 조합은 총 3가지이다.

1. 세트로만 구매

2. 낱개로만 구매

3. 섞어서 구매

이렇게 3가지 경우를 가장 저렴한 세트와 낱개로 계산하여 가장 작은 총합을 구하면 답이다.