https://www.acmicpc.net/problem/2579
2579번: 계단 오르기
계단 오르기 게임은 계단 아래 시작점부터 계단 꼭대기에 위치한 도착점까지 가는 게임이다. <그림 1>과 같이 각각의 계단에는 일정한 점수가 쓰여 있는데 계단을 밟으면 그 계단에 쓰여 있는 점
www.acmicpc.net
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int[] stair = new int[n+1];
int[] memo = new int[n+1];
for(int i=1; i<=n; i++) {
st = new StringTokenizer(br.readLine());
stair[i] = Integer.parseInt(st.nextToken());
}
memo[1] = stair[1];
if(n>=2) {
memo[2] = stair[2]+stair[1];
if(n>=3) {
memo[3] = Math.max(stair[2], stair[1])+stair[3];
if(n>=4) {
for(int i=4; i<=n; i++) {
memo[i] = Math.max(memo[i-3]+stair[i-1], memo[i-2])+stair[i];
}
}
}
}
bw.write(memo[n]+"");
bw.close();
}
}
'백준' 카테고리의 다른 글
백준 이분탐색 - 1654번 : 랜선 자르기 (0) | 2022.02.28 |
---|---|
백준 동적계획법 - 1463번 : 1로 만들기 (0) | 2022.02.27 |
백준 동적계획법 - 1932번 : 정수 삼각형 (0) | 2022.02.17 |
백준 동적계획법 - 1149번 : RGB 거리 (1) | 2022.02.17 |
백준 동적계획법 - 9461번 : 파도반 수열 (0) | 2022.02.16 |