-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_1932.java
More file actions
58 lines (50 loc) ยท 1.34 KB
/
_1932.java
File metadata and controls
58 lines (50 loc) ยท 1.34 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package backjoon;
// https://www.acmicpc.net/problem/1932
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class _1932 {
static int[][] arr; //์ผ๊ฐํ์ด ์ ์ฅ๋๋ 2์ฐจ์๋ฐฐ์ด
static Integer[][] dp;
static int N;
// memory 26600 runtime 260
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
arr = new int[N][N];
dp = new Integer[N][N];
StringTokenizer st;
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
for (int j = 0; j < i + 1; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 0; i < N; i++) {
dp[N - 1][i] = arr[N - 1][i];
}
System.out.println(func(0, 0));
}
static int func(int depth, int idx) {
// ๋ง์ง๋ง ํ์ผ ๊ฒฝ์ฐ ํ์ฌ ์์น์ dp๊ฐ ๋ฐํ
if(depth == N - 1)
return dp[depth][idx];
// ํ์ํ์ง ์์๋ ๊ฐ์ผ ๊ฒฝ์ฐ ๋ค์ ํ์ ์์ชฝ ๊ฐ ๋น๊ต
if (dp[depth][idx] == null) {
dp[depth][idx] = Math.max(func(depth + 1, idx), func(depth + 1, idx + 1)) + arr[depth][idx];
}
return dp[depth][idx];
}
}
/*
input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
output
30
*/