[Algorithm] BOJ 2156 - 포도주 시식

image

백준온라인저지 2156번 - 포도주 시식 image

public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();

        Integer[] wines = new Integer[n];

        Integer[] Dp = new Integer[n+1];
        for( int i = 0 ; i < n ; i ++ ){
            wines[i] = scan.nextInt();
        }

        Dp[0] = 0;
        for( int i = 1 ; i <= n ; i++){
            if( i == 1 ) { Dp[i] = wines[i-1]; continue; }
            if( i == 2 ) { Dp[i] = wines[i-2] + wines[i-1]; continue; }

            Dp[i] = Dp[i-1] >= Dp[i-2] + wines[i-1] ? Dp[i-1] : Dp[i-2] + wines[i-1];
            Dp[i] = Dp[i] >= Dp[i-3] + wines[i-1] + wines[i-2] ? Dp[i] : Dp[i-3] + wines[i-1] + wines[i-2];
        }

        System.out.println(Dp[n]);



    }


© 2021. All rights reserved.

Powered by Hydejack v9.1.2