forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2.java
36 lines (33 loc) Β· 1.17 KB
/
2.java
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
import java.util.*;
public class Main {
static int n;
static int[][] dp = new int[500][500]; // λ€μ΄λλ―Ή νλ‘κ·Έλλ°μ μν DP ν
μ΄λΈ μ΄κΈ°ν
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < i + 1; j++) {
dp[i][j] = sc.nextInt();
}
}
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°μΌλ‘ 2λ²μ§Έ μ€λΆν° λ΄λ €κ°λ©΄μ νμΈ
for (int i = 1; i < n; i++) {
for (int j = 0; j <= i; j++) {
int upLeft, up;
// μΌμͺ½ μμμ λ΄λ €μ€λ κ²½μ°
if (j == 0) upLeft = 0;
else upLeft = dp[i - 1][j - 1];
// λ°λ‘ μμμ λ΄λ €μ€λ κ²½μ°
if (j == i) up = 0;
else up = dp[i - 1][j];
// μ΅λ ν©μ μ μ₯
dp[i][j] = dp[i][j] + Math.max(upLeft, up);
}
}
int result = 0;
for (int i = 0; i < n; i++) {
result = Math.max(result, dp[n - 1][i]);
}
System.out.println(result);
}
}