import sys
r = sys.stdin.readline
n = int(r())
DP = [ list(map(int, r().rstrip().split())) for _ in range(n)]
step = 2
for i in range(1,n):
for j in range(step):
if j == 0:
DP[i][j] = DP[i][j] + DP[i-1][j]
elif i == j:
DP[i][j] = DP[i][j] + DP[i-1][j-1]
else:
DP[i][j] = DP[i][j] + max(DP[i-1][j-1], DP[i-1][j])
step += 1
print(max(DP[n-1]))
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[][] DP = new int[n][n];
StringTokenizer st;
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j <= i; j++) {
DP[i][j] = Integer.parseInt(st.nextToken());
}
}
int step = 2;
for (int i = 1; i < n; i++) {
for (int j = 0; j < step; j++) {
if (j == 0) {
DP[i][j] = DP[i][j] + DP[i - 1][j];
} else if (i == j) {
DP[i][j] = DP[i][j] + DP[i - 1][j - 1];
} else {
DP[i][j] = DP[i][j] + Math.max(DP[i - 1][j - 1], DP[i - 1][j]);
}
}
step += 1;
}
int maxSum = 0;
for (int i = 0; i < n; i++) {
maxSum = Math.max(maxSum, DP[n - 1][i]);
}
System.out.println(maxSum);
}
}
Leave a comment