https://www.acmicpc.net/problem/2217
다음 문제는 로프를 하나 사용해도 되고 2개, 3개 아니면 모두 사용해도 된다.
그리고 그 중에서 최대로 들어올릴 수 있는 최대 중량을 출력하는 문제이다.
예를 들어 3 10 15 60 을 입력으로 받았을 때
들어올릴 수 있는 중량은 60 로프만 선택했을 때, 6060, 15 로프 선택했을 때, 30 (15 x 2 )60, 15, 10 로프 선택했을 때, 30 (10 x 3)
이므로 최대 중량은 60이 출력된다.
반례
3
10
15
60
출력 : 60
-----------------
4
2
3
4
100
출력 : 100
-----------------
2
10
15
출력 : 20
-----------------
4
3
3
3
10
출력 : 12
-----------------
코드
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
Integer arr[] = new Integer[N];
int max = 0;
for(int i=0;i<N;i++)
{
st = new StringTokenizer(br.readLine());
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr,Collections.reverseOrder());
for(int i=0;i<N;i++)
{
max = Math.max(max, arr[i]*(i+1));
}
System.out.println(max);
}
}
'알고리즘 > 백준 풀이(그리디)' 카테고리의 다른 글
백준 14916번 - 거스름돈 (0) | 2024.01.23 |
---|---|
백준 1946번 - 신입 사원 (0) | 2024.01.19 |
백준 1026번 - 보물 (0) | 2024.01.17 |
백준 11047번 - 동전 0 (0) | 2024.01.16 |
백준 11399번 - ATM (0) | 2024.01.16 |