SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
#include <iostream>
#include <algorithm>
using namespace std;
int N, M;
int weight[1000];
int getResult() {
int cmp = 2e9;
sort(weight, weight+N);
// 두개씩 고른다.
for(int i=0; i<N; i++) {
if(weight[i] > M) {
return cmp==2e9? -1:M-cmp;
}
for(int j=i+1; j<N; j++) {;
if(weight[i]+weight[j] > M) {
break;
}
if(cmp > (M-weight[i]-weight[j])) {
cmp = M-weight[i]-weight[j];
}
}
}
return cmp==2e9? -1:M-cmp;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc; cin >> tc;
for(int t=1; t<=tc; t++ ){
cin >> N >> M;
for(int i=0; i<N; i++) {
cin >> weight[i];
}
cout << '#' << t << ' ' << getResult() << '\n';
}
return 0;
}
'알고리즘 > SWEA' 카테고리의 다른 글
[SWEA] 8457. 알 덴테 스파게티 (0) | 2020.02.17 |
---|---|
[SWEA] 8888. 시험 (0) | 2020.02.16 |
[SWEA] 7728. 다양성 측정 (0) | 2019.06.29 |
[SWEA] 6692. 다솔이의 월급 상자 (*소수점 자릿 수 조정) (0) | 2019.05.07 |
[SWEA] 5603. 건초더미 (0) | 2019.05.06 |
댓글