백준
[백준] 2579 계단 오르기 [자바]
거북이같은곰
2021. 12. 3. 20:22
https://st-lab.tistory.com/132
[백준] 2579번 : 계단 오르기 - JAVA [자바]
www.acmicpc.net/problem/2579 과 같이 각각의 계단에는 일정한 점수가 쓰여 있는데 계단을 밟으면 그 계단에 쓰여 있는 점" data-og-host="www.acmicpc.net" data-og-source-url="https://www.acmicpc.net/proble..
st-lab.tistory.com
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static int[] arr;
static int[] stair = null;
public static void main(String arg[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
stair = new int[N+1];
arr = new int[N+1];
for(int i = 1; i <= N; i++)
arr[i] = Integer.parseInt(br.readLine());
stair[1] = arr[0];
stair[0] = 0;
dp(N);
System.out.println(stair[N]);
}
static int dp(int n) {
if(n <= 0)
return 0;
if(stair[n] == 0) {
stair[n] = Math.max(dp(n-2), dp(n-3)+arr[n-1]) + arr[n];
return stair[n];
}
else
return stair[n];
}
}