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


[ 알고리즘풀이 ]

 브랜드 중에서 패키지가 제일 싼 가격과 낱개가 제일 싼 가격을 뽑아낸 후, N 개를 모두 낱개로 사는 경우, N 개를 모두 패키지로 사는 경우, N 개를 최대한 패키지로 사고 남은 부분을 낱개로 사는 경우 중 가장 작은 값을 출력해주면 됩니다.

#include<iostream>
#include<algorithm>
using namespace std;

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int N, M, a, b, six = 987654321, one = 987654321, ans = 987654321;
	cin >> N >> M;
	for (int i = 0; i < M; i++) {
		cin >> a >> b;
		six = min(six, a);
		one = min(one, b);
	}
	ans = min(ans, one * N);
	if (N % 6 == 0)
		ans = min(ans, six * (N / 6));
	else{
		ans = min(ans, six * (N / 6) + one * (N - 6 *(N / 6)));
		ans = min(ans, six * (N / 6 + 1));
	}
	cout << ans;
	return 0;
}

 

+ Recent posts