File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package backjoon ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .IOException ;
5+ import java .io .InputStreamReader ;
6+
7+ public class _2579 {
8+ public static void main (String [] args ) throws IOException {
9+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10+
11+ int N = Integer .parseInt (br .readLine ());
12+
13+ int [] DP = new int [N + 1 ];
14+ int [] arr = new int [N + 1 ];
15+
16+ for (int i = 1 ; i <= N ; i ++) {
17+ arr [i ] = Integer .parseInt (br .readLine ());
18+ }
19+
20+ // index = 0 ์ ์์์ ์ด๋ค.
21+ DP [1 ] = arr [1 ];
22+
23+ // N ์ด 1์ด ์
๋ ฅ๋ ์๋ ์๊ธฐ ๋๋ฌธ์ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ค ํ์๊ฐ ์๋ค.
24+ if (N >= 2 ) {
25+ DP [2 ] = arr [1 ] + arr [2 ];
26+ }
27+
28+ for (int i = 3 ; i <= N ; i ++) {
29+ DP [i ] = Math .max (DP [i - 2 ], DP [i - 3 ] + arr [i - 1 ]) + arr [i ];
30+ }
31+
32+ System .out .println (DP [N ]);
33+
34+ }
35+ }
You canโt perform that action at this time.
0 commit comments