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

Appearance settings

Latest commit

 

History

History
History
36 lines (33 loc) · 1.17 KB

File metadata and controls

36 lines (33 loc) · 1.17 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.