자바로 백준 1049 풀기

hong030·2023년 4월 9일
0
  • 실버 4단계 문제

풀이)
낱개로 사는 게 쌀지, 패키지로 사는 게 쌀지 비교하여 구하면 된다.

내 코드)

import java.util.Scanner;
import java.util.Arrays;
 
 
public class Main {
    public static void main(String[] args) throws Exception {
 
        Scanner sc = new Scanner(System.in);
        
        int N = sc.nextInt();
        int M =sc.nextInt();
        int Min = Integer.MAX_VALUE;
 
        int[] unit = new int[M];
        int[] pack = new int[M];
        for(int i=0; i<M; i++){
            pack[i] = sc.nextInt();
            unit[i] = sc.nextInt();
        }
        Arrays.sort(unit);
        Arrays.sort(pack);
        
        //가장 싼 6개짜리 팩 구매 vs 가장 싼 낱개 구매 vs (가장싼 6개짜리 팩 + 낱개)
        Min = Math.min(((N/6)+1)*pack[0], N*unit[0]);    
        Min = Math.min(Min, ((N/6))*pack[0] + (N%6)*unit[0]);
        
        System.out.println(Min);
    }
}

profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글