[BOJ] 2156 포도주 시식

Time Lapse :NONE

2156.cpp

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <memory.h>
#include <algorithm>
using namespace std;

int N;
long long ans=0;
long long dp[10001][2];
long long arr[10001];
long long dy(int n,int time)
{
if(n>N)
return 0;
long long &ret = dp[n][time];
if(ret!=-1)
return ret;

ret = 0;
if(time==0)
{
ret = max(ret,dy(n+1,1));
ret = max(ret,dy(n+2,0));
}
if(time==1) {
ret = max(ret, dy(n + 2, 0));
ret = max(ret, dy(n+3,0));
}

ret = ret + arr[n];
return ret;
}
int main(void) {
cin>>N;
for(int i=1;i<=N;i++)
cin>>arr[i];

memset(dp,-1,sizeof(dp));
ans = dy(1,0);
ans = max(ans, dy(2,0));
cout<<ans<<endl;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2156/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.