Baekjoon Online Judge - 2156
Review
Code (JAVA)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| import java.util.*;
public class Main { public static int Max(int a, int b, int c) { if(b > c) { return a > b ? a : b; } else { if(a > c) { return a; } else { return c; } } } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] wine = new int[10001]; int[] dp = new int[10001]; for(int i = 1; i <= N; i++) { wine[i] = sc.nextInt(); }
dp[0] = 0; dp[1] = wine[1]; dp[2] = dp[1] + wine[2]; for(int i = 3; i <= N; i++) { dp[i] = Max(dp[i-1], wine[i] + dp[i-2], wine[i] + wine[i-1] + dp[i-3]); } System.out.println(dp[N]); sc.close(); } }
|