백준
1. Python
참고1, 참고2
import copy
import sys
input = sys.stdin.readline
n = int(input())
for i in range(n):
if i == 0:
dp_max = list(map(int, input().split()))
dp_max = copy.deepcopy(dp_max)
dp_min = copy.deepcopy(dp_max)
else:
new = list(map(int, input().split()))
a, b, c = dp_max[0], dp_max[1], dp_max[2]
dp_max[0] = new[0] + max(a, b)
dp_max[1] = new[1] + max(a, b, c)
dp_max[2] = new[2] + max(b, c)
a, b, c = dp_min[0], dp_min[1], dp_min[2]
dp_min[0] = new[0] + min(a, b)
dp_min[1] = new[1] + min(a, b, c)
dp_min[2] = new[2] + min(b, c)
print(max(dp_max), min(dp_min))
import sys
input = sys.stdin.readline
n = int(input())
max_dp = [0] * 3
min_dp = [0] * 3
max_tmp = [0] * 3
min_tmp = [0] * 3
for i in range(n):
a, b, c = map(int, input().split())
for j in range(3):
if j == 0:
max_tmp[j] = a + max(max_dp[j], max_dp[j + 1])
min_tmp[j] = a + min(min_dp[j], min_dp[j + 1])
elif j == 1:
max_tmp[j] = b + max(max_dp[j - 1], max_dp[j], max_dp[j + 1])
min_tmp[j] = b + min(min_dp[j - 1], min_dp[j], min_dp[j + 1])
else:
max_tmp[j] = c + max(max_dp[j], max_dp[j - 1])
min_tmp[j] = c + min(min_dp[j], min_dp[j - 1])
for j in range(3):
max_dp[j] = max_tmp[j]
min_dp[j] = min_tmp[j]
print(max(max_dp), min(min_dp))