BOJ::2217 로프

https://www.acmicpc.net/problem/2217


각각의 상황에서 최선의 선택에 대해 최대값을 찾아주면 된다.


<JAVA>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
 
public class Main {
 
    public static void main(String args[]) throws NumberFormatException, IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int a[]= new int[n+1];
        for(int i = 1 ; i <= n ; i++){
            a[i]=Integer.parseInt(br.readLine());
        }
        
        Arrays.sort(a);
        
        int max = 0;
        for(int i = 1 ; i <= n ; i++){
            if(a[i]*(n-i+1> max){
                max = a[i]*(n-i+1);
            }
        }
        System.out.println(max);
    }
}
cs



<C++>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
    int n;
    cin >> n;
    int *= new int[n ];
 
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
 
    sort(a, a + n);
 
    int max = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] * (n - i) > max) {
            max = a[i] * (n - i);
        }
    }
    cout << max << endl;
}
cs


'BOJ::문제풀이' 카테고리의 다른 글

2309 일곱 난쟁이  (0) 2018.01.01
2293 동전 1  (0) 2018.01.01
2206 벽 부수고 이동하기  (2) 2018.01.01
2193 이친수  (0) 2018.01.01
2178 미로 탐색  (0) 2018.01.01

+ Recent posts