Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d29adf0

Browse files
issue sowon-dev#43 2579
1 parent 7c955fc commit d29adf0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎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)

AltStyle によって変換されたページ (->オリジナル) /