알고리즘

백준 20162번 간식 파티 (JAVA)

박카스마시며코딩 2023. 3. 1. 17:09

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

 

20162번: 간식 파티

서울이는 입맛이 까다로운 고양이다. 입맛이 까다로운 서울이는 전에 먹었던 간식보다 더 맛있는 간식만 먹는다. 서울이는 간식의 평점이 높을수록 맛있다고 느낀다. 집사는 서울이에게 N 일

www.acmicpc.net

 

 

 

package BOJ.dp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.function.Function;

public class BOJ_20162 {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Function<String,Integer> stoi = Integer::parseInt;
        int n = stoi.apply(br.readLine());
        int[] input = new int[n];
        for(int i = 0 ; i < n ; i++){
            input[i] = stoi.apply(br.readLine());
        }
        int result = cal(input,n);
        System.out.println(result);
    }

    private static int cal(int[] input, int n) {
        int[] dp = Arrays.copyOf(input,n);
        int result= 0;
        for(int i = 0 ; i < n ; i++){
            result = Math.max(result,input[i]);
            for(int j = i+1 ; j < n ; j++){
                if(input[i] < input[j] && dp[i] + input[j] > dp[j]){
                    dp[j] = dp[i] + input[j];
                    result = Math.max(result,dp[j]);
                }
            }
        }
        return result;
    }
}