File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ package backjoon ;
2+ // https://www.acmicpc.net/problem/1932
3+
4+ import java .io .BufferedReader ;
5+ import java .io .InputStreamReader ;
6+ import java .io .IOException ;
7+ import java .util .StringTokenizer ;
8+
9+ public class _1932 {
10+ static int [][] arr ; //์ผ๊ฐํ์ด ์ ์ฅ๋๋ 2์ฐจ์๋ฐฐ์ด
11+ static Integer [][] dp ;
12+ static int N ;
13+
14+ // memory 26600 runtime 260
15+ public static void main (String [] args ) throws IOException {
16+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
17+ N = Integer .parseInt (br .readLine ());
18+ arr = new int [N ][N ];
19+ dp = new Integer [N ][N ];
20+ StringTokenizer st ;
21+ for (int i = 0 ; i < N ; i ++) {
22+ st = new StringTokenizer (br .readLine (), " " );
23+
24+ for (int j = 0 ; j < i + 1 ; j ++) {
25+ arr [i ][j ] = Integer .parseInt (st .nextToken ());
26+ }
27+ }
28+ for (int i = 0 ; i < N ; i ++) {
29+ dp [N - 1 ][i ] = arr [N - 1 ][i ];
30+ }
31+
32+ System .out .println (func (0 , 0 ));
33+ }
34+ static int func (int depth , int idx ) {
35+ // ๋ง์ง๋ง ํ์ผ ๊ฒฝ์ฐ ํ์ฌ ์์น์ dp๊ฐ ๋ฐํ
36+ if (depth == N - 1 )
37+ return dp [depth ][idx ];
38+
39+ // ํ์ํ์ง ์์๋ ๊ฐ์ผ ๊ฒฝ์ฐ ๋ค์ ํ์ ์์ชฝ ๊ฐ ๋น๊ต
40+ if (dp [depth ][idx ] == null ) {
41+ dp [depth ][idx ] = Math .max (func (depth + 1 , idx ), func (depth + 1 , idx + 1 )) + arr [depth ][idx ];
42+ }
43+ return dp [depth ][idx ];
44+
45+ }
46+ }
47+ /*
48+ input
49+ 5
50+ 7
51+ 3 8
52+ 8 1 0
53+ 2 7 4 4
54+ 4 5 2 6 5
55+
56+ output
57+ 30
58+ */
You canโt perform that action at this time.
0 commit comments