Skip to content

Commit d29adf0

Browse files
committed
issue #43 2579
1 parent 7c955fc commit d29adf0

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

โ€Žsrc/backjoon/_2579.javaโ€Ž

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
ย (0)