코딩테스트
[BOJ] 2579 계단 오르기
Eun
2025. 3. 19. 20:55
package project;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int dp[] = new int[n+1];
int step[]=new int[n+1];
int max = 0;
int result=0;
for(int i=1;i<=n;i++) {
step[i] = Integer.parseInt(br.readLine());
}
dp[1]=step[1];
if(n>=2) dp[2]=step[1]+step[2];
for(int j=3;j<=n;j++) {
// 두 계단 전에서 오는 경우
// 세 계단에서 한칸 건너고 오는 경우
dp[j]=Math.max(dp[j-2], dp[j-3]+step[j-1])+step[j];
}
for(int i=1;i<=n;i++) {
result = Math.max(max, dp[i]);
}
System.out.println(result);
}
}