코드와이
[BAEKJOON] 2579. 계단 오르기 본문
DP문제
문제풀이
2579번: 계단 오르기
계단 오르기 게임은 계단 아래 시작점부터 계단 꼭대기에 위치한 도착점까지 가는 게임이다. <그림 1>과 같이 각각의 계단에는 일정한 점수가 쓰여 있는데 계단을 밟으면 그 계단에 쓰여 있는 점
www.acmicpc.net
package acmicpc.Silver3;
import java.util.Scanner;
public class 계단_오르기 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] sta = new int[301];
int n = sc.nextInt();
if( n == 1) {
System.out.println(sc.nextInt());
}
else {
for(int i = 1 ; i <= n ; i++) {
sta[i] = sc.nextInt();
}
int[] ans = new int[301];
ans[1] = sta[1];
ans[2] = sta[2] + sta[1];
for(int i = 3 ; i <= n ; i++) {
ans[i] = Math.max(sta[i] + sta[i-1] + ans[i-3], sta[i] + ans[i-2]);
}
System.out.println(ans[n]);
}
sc.close();
}
}
'acmicpc' 카테고리의 다른 글
[BAEKJOON] 2805. 나무 자르기 (0) | 2021.02.22 |
---|---|
[BAEKJOON] 17070. 파이프 옮기기1 (0) | 2021.02.19 |
[BAEKJOON] 2606. 바이러스 (0) | 2021.02.19 |
[BAEKJOON] 17136. 색종이 붙이기 (0) | 2021.02.19 |
[BAEKJOON] 2193. 이친수 (0) | 2021.02.19 |