#include <stdio.h>

int juice[10001];
int dp[10001];
int max(int a,int b){
    return a>b ? a:b; 
}

int bottom_up(int n)  {
    //기저
    dp[1]=juice[1];
    dp[2]=juice[1]+juice[2];
    dp[3]=max(dp[2],max(juice[1]+juice[3], juice[2]+juice[3]));

    //3개 연속 안됨
    //i 넘기기,i마시고 i-1 넘기기,i마시고 i-1 마시기(i-3 넘기기)
    for(int i=4;i<=n; i++) {
        dp[i]=max(dp[i-1],max(dp[i-2]+juice[i],juice[i-1]+juice[i]+dp[i-3]));
    }
    return dp[n];
}

int main() {
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++) {
        scanf("%d",&juice[i]);
    }
    printf("%d",bottom_up(n));
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: