The Kkang's man

[ 자바 /Java ] 백준 2579 : 계단오르기 본문

알고리즘/DP

[ 자바 /Java ] 백준 2579 : 계단오르기

정낑깡 2021. 7. 18. 20:39

문제


 


 

풀이


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Stair_2579 {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(br.readLine());
		
		int[] stairs = new int[301];
		int[] score = new int[301];
		
		int i;
		for(i=1; i<=n; i++) {
			stairs[i] = Integer.parseInt(br.readLine());
		}
		
		score[1] = stairs[1];
		score[2] = stairs[1] + stairs[2];
		score[3] = Math.max(stairs[1], stairs[2]) + stairs[3];
				
		for(i=4; i<=n; i++) {
			score[i] = Math.max(score[i-3] + stairs[i-1], score[i-2]) + stairs[i];
		}
		System.out.println(score[n]);
	}
}
Comments