package baek.silver3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main_2579계단오르기 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] map = new int[N];
int[][] dp = new int[N+1][3];
for(int n=0; n<N; n++){
map[n] = Integer.parseInt(br.readLine());
}
dp[0][1] = map[0];
if(N==1){
System.out.println(dp[0][1]);
return;
}
dp[1][1] = map[1]; //2칸 한번에 점프
dp[1][2] = dp[0][1]+map[1]; //0번 칸에서 1칸 점프
if(N==2){
System.out.println(Math.max(dp[N-1][2], dp[N-1][1]));
return;
}
dp[2][1] = dp[0][1]+map[2];
dp[2][2] = dp[1][1]+map[2];
int idx = 3;
while(idx <=N-1){
dp[idx][1] = Math.max(dp[idx-2][1]+map[idx], dp[idx-2][2]+map[idx]);
dp[idx][2] = dp[idx-1][1]+map[idx];
idx++;
}
System.out.println(Math.max(dp[N-1][2], dp[N-1][1]));
}
}
'Learning-log > Algorithm 문풀' 카테고리의 다른 글
백준 12904번. A와B (Java) (0) | 2024.04.12 |
---|---|
백준 1240번. 노드사이의 거리 (Java) (0) | 2024.02.16 |
백준 17182번 우주탐사(JAVA) (1) | 2023.09.30 |
(Java)백준 1991. 트리 순회 (0) | 2023.06.20 |
SWEA - 3124 최소 스패닝 트리 (0) | 2023.04.16 |