[BOJ] 2579 계단 오르기

Time Lapse :NONE

2579.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
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdio>

using namespace std;

#define MAXSIZE 1000

int N;
long long cache[301][3];
int stairs[301];
long long ans = 0;

long long dp(int idx,int stat)
{
if (idx > N)
return -34567890;

if (idx == N)
return stairs[idx];
long long &ret = cache[idx][stat];

if (ret != -1)
return ret;

ret = 0;

if (stat == 2)
ret = dp(idx + 2, 1);
else
ret = max(dp(idx + 2, 1), dp(idx + 1, 2));

ret += stairs[idx];

return ret;

}

int main(void)
{
cin >> N;
memset(cache, -1, sizeof(cache));
stairs[0] = 0;
for (int i = 1; i <= N; i++)
cin >> stairs[i];
ans = max(dp(1, 1),dp(2,1));
cout << ans << endl;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2579/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.