[백준] 2798 블랙잭
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int N, M;
cin >> N >> M;
vector<int> card;
for (int i = 0; i < N; ++i) {
int input;
cin >> input;
card.push_back(input);
}
vector<int> sum;
for (int i = 0; i < N - 2; ++i) {
for (int j = i + 1; j < N - 1; ++j) {
for (int k = j + 1; k < N; ++k) {
sum.push_back(card[i] + card[j] + card[k]);
}
}
}
sort(sum.begin(), sum.end());
int left = 0;
int right = sum.size() - 1;
while (left < right) {
int mid = (left + right + 1) / 2;
if (sum[mid] > M) right = mid - 1;
else left = mid;
}
cout << sum[right];
return 0;
}
1년 전 코드
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> card;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
int input;
cin >> input;
card.push_back(input);
}
sort(card.begin(), card.end());
int best = 0;
for (int i = 0; i < n; ++i) {
int j = 0;
int k = n - 1;
while (j < k) {
if (j == i) {
j++;
continue;
}
if (k == i) {
k--;
continue;
}
int sum = card[i] + card[j] + card[k];
if (sum > m) {
k--;
}
if(sum == m){
cout << m;
return 0;
}
if (sum < m) {
best = max(best, sum);
j++;
}
}
}
cout << best;
return 0;
}