문제는 여기에서 확인
풀이
임의로 몇 개의 로프를 골라서 사용해도 된다 => 무거운 중량만 들 수 있는 로프를 골라서 쓰면 되겠다 => 로프를 중량 별로 내림차순 정렬한 후, 이전 최대중량보다 작아질 때 연산을 멈춘다
이 때 로프 집합에 속한 로프 중 가장 최소 중량이 걸리는 로프가 지탱 중인 중량이 ‘최대 중량’이 됩니다.
결국 최대 중량 = (로프-중량 집합 중 가장 적은 중량)*(로프의 갯수)입니다.
코드
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;
int N;
int main()
{
int ans = -1;
cin >> N;
vector<int> rope_w;
while (N--) {
int w;
cin >> w;
rope_w.push_back(w);
}
sort(rope_w.begin(), rope_w.end(), greater<>());
int cnt = 1;
vector<int>::iterator it;
for (it = rope_w.begin(); it < rope_w.end(); it++) {
ans = max(ans, (*it)*cnt);
cnt++;
}
cout << ans;
return 0;
}