https://www.acmicpc.net/problem/1932
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
public class Main {
static int num;
static int[][] triangle;
static Integer[][] dp;
public int findMaxNum(int depth, int index) {
if(depth == num - 1) {
return dp[depth][index];
}
if(dp[depth][index] == null) {
dp[depth][index] = Math.max(findMaxNum(depth + 1, index), findMaxNum(depth + 1, index + 1)) + triangle[depth][index];
}
return dp[depth][index];
}
public int getMaxNum() {
for(int i = 0; i < triangle.length; i++) {
dp[dp.length - 1][i] = triangle[dp.length - 1][i];
}
return findMaxNum(0, 0);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
num = Integer.parseInt(br.readLine());
triangle = new int[num][num];
dp = new Integer[num][num];
for(int i = 0; i < num; i++) {
String[] input = br.readLine().split(" ");
for(int j = 0; j < input.length; j++) {
triangle[i][j] = Integer.parseInt(input[j]);
}
}
br.close();
Main m = new Main();
bw.write(m.getMaxNum() + "\n");
bw.flush();
bw.close();
}
}
public int findMaxNum(int depth, int index) {
if(depth == num - 1) {
return dp[depth][index];
}
if(dp[depth][index] == null) {
dp[depth][index] = Math.max(findMaxNum(depth + 1, index), findMaxNum(depth + 1, index + 1)) + triangle[depth][index];
}
return dp[depth][index];
}