PS
BOJ 2437 : 저울
lickelon
2024. 7. 30. 22:41
- 문제 링크 : boj.kr/2437
- 난이도 : G2
- 태그 : 그리디, 정렬
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<int> arr(n);
for(auto &u : arr) cin >> u;
sort(all(arr));
int sum = 0;
for(auto u : arr) {
if(sum + 1 < u) {
break;
}
sum += u;
}
cout << sum + 1;
return 0;
}
풀이
1~K까지 모든 수를 만들 수 있고, K+1보다 작은 어떤 수 x가 추가되면 1~K+x까지의 모든 수를 만들 수 있다.
x를 작은 순으로 추가하는 것이 최적이다.
728x90