백준
1. 다이나믹 프로그래밍
n = int(input())
dp = []
for _ in range(n):
dp.append(list(map(int, input().split())))
for i in range(1, n):
for j in range(i + 1):
if j == 0:
up_left = 0
else:
up_left = dp[i - 1][j - 1]
if j == i:
up = 0
else:
up = dp[i - 1][j]
dp[i][j] = dp[i][j] + max(up_left, up)
print(max(dp[n - 1]))
2. C++
#include <bits/stdc++.h>
using namespace std;
int n;
int dp[500][500];
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i + 1; j++) {
cin >> dp[i][j];
}
}
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] + max(upLeft, up);
}
}
int result = 0;
for (int i = 0; i < n; i++) {
result = max(result, dp[n - 1][i]);
}
cout << result << '\n';
}