[백준] 로프 2217번 - Java

GoshK·2022년 2월 16일
0

[백준] Java

목록 보기
31/49
post-thumbnail

[백준] 로프 2217번

나의 풀이

public class Rope {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int[] weights = new int[N];
        for(int i = 0; i < N; i++) {
            weights[i] = Integer.parseInt(br.readLine());
        }

        Arrays.sort(weights);

        int maxWeight = -1;
        for(int i = 0; i < N; i++) {
            maxWeight = Math.max(maxWeight, weights[i] * (N - i));
        }

        System.out.println(maxWeight);
    }
}
  • 각각 N개의 로프를 사용했을 때 최대 무게가 주어진다. 최대 무게가 담긴 배열을 오름차순으로 정렬한다. 각각의 로프에는 최대 무게/로프의 갯수 무게가 달려있다. 그렇기 때문에 N개부터 -1을 하면서 로프 갯수와 무게를 곱해주면서 이 무게들 중 가장 무거운 무게를 찾아 출력하면 된다.

0개의 댓글