File tree 1 file changed +45
-0
lines changed
1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ package backjoon ;
2
+ // https://www.acmicpc.net/problem/2156
3
+
4
+ import java .io .BufferedReader ;
5
+ import java .io .IOException ;
6
+ import java .io .InputStreamReader ;
7
+
8
+ public class _2156 {
9
+ public static void main (String [] args ) throws IOException {
10
+
11
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
12
+ int N = Integer .parseInt (br .readLine ());
13
+
14
+ int [] arr = new int [N +1 ];
15
+ int [] dp = new int [N +1 ];
16
+
17
+ for (int i = 1 ; i <= N ; i ++){
18
+ arr [i ] = Integer .parseInt (br .readLine ());
19
+ }
20
+
21
+ dp [1 ] = arr [1 ];
22
+ if (N > 1 ) {
23
+ dp [2 ] = arr [1 ] + arr [2 ];
24
+ }
25
+ for (int i = 3 ; i <= N ; i ++) {
26
+ dp [i ] = Math .max (dp [i - 1 ], Math .max (dp [i - 2 ] + arr [i ], dp [i - 3 ] + arr [i - 1 ] + arr [i ]));
27
+
28
+ }
29
+ System .out .println (dp [N ]);
30
+
31
+ }
32
+ }
33
+ /*
34
+ input
35
+ 6
36
+ 6
37
+ 10
38
+ 13
39
+ 9
40
+ 8
41
+ 1
42
+
43
+ output
44
+ 33
45
+ */
You can’t perform that action at this time.
0 commit comments