728x90
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static Integer dp[];
static int arr[];
public static void main(String[] args) throws Exception, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
dp = new Integer[N + 1];
arr = new int[N + 1];
for(int i = 1; i <= N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
dp[0] = arr[0];
dp[1] = arr[1];
if(N >= 2) {
dp[2] = arr[1] + arr[2];//find(2) 하면 find(0), find(-1)을 찾게됨
}
System.out.println(find(N));
}
static int find(int N) {
if(dp[N] == null) {
dp[N] = Math.max(find(N-2), find(N-3) + arr[N-1]) + arr[N];
}
return dp[N];
}
}
728x90
'CS > Algorithm' 카테고리의 다른 글
[백준 : 11726] 2 x n 타일링 - JAVA (0) | 2021.05.16 |
---|---|
[백준 : 1932] 정수 삼각형 - java (0) | 2021.05.16 |
[백준 : 2156] 포도주 시식 - java (0) | 2021.05.16 |
[백준 : 11053] 가장 긴 증가하는 부분 수열 (0) | 2021.05.16 |
[백준 2749] 피보나치 수3 (0) | 2021.05.10 |