Problem:
 Score : 100 points    Problem Statement Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.  The vacation consists of N days.
For each i ( 1 \leq i \leq N ), Taro will choose one of the following activities and do it on the i -th day:   A: Swim in the sea. Gain a_i points of happiness.  B: Catch bugs in the mountains. Gain b_i points of happiness.  C: Do homework at home. Gain c_i points of happiness.   As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.  Find the maximum possible total points of happiness that Taro gains.      Constraints  All values in input are integers.  1 \leq N \leq 10^5  1 \leq a_i, b_i, c_i \leq 10^4         Input Input is given from Standard Input in the following format:  N  a_1  b_1  c_1  a_2  b_2  c_2  :  a_N  b_N  c_N       Output Print the maximum possible total points of happiness that Taro gains.        Sample Input 1 3
10 40 70
20 50 80
30 60 90      Sample Output 1 210  If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.       Sample Input 2 1
100 10 1      Sample Output 2 100       Sample Input 3 7
6 7 8
8 8 3
2 5 2
7 8 6
4 6 8
2 3 4
7 5 1      Sample Output 3 46  Taro should do activities in the order C, A, B, A, C, B, A.

Solution (Python):


N = int(input())

abc = [[int(i) for i in input().split()] for j in range(N)]



dp = [[0]*3 for i in range(N+1)]

for i in range(N):
    for j in range(3):
        for k in range(3):
            if j == k:
                continue
            dp[i+1][k] = max(dp[i+1][k], dp[i][j]+abc[i][k])

answer = 0
for i in range(3):
    answer = max(answer, dp[-1][i])
print(answer)

Solution (Python):
def main():
    n=int(input())
    a=[]
    for _ in range(n):
        a.append(list(map(int,input().split())))
    dp=[]
    dp.append([a[0][0],a[0][1],a[0][2]])
    for i in range(1,n):
        tmpa=max(dp[i-1][1],dp[i-1][2])+a[i][0]
        tmpb=max(dp[i-1][0],dp[i-1][2])+a[i][1]
        tmpc=max(dp[i-1][0],dp[i-1][1])+a[i][2]
        dp.append([tmpa,tmpb,tmpc])
    print(max(dp[n-1]))
if __name__=='__main__':
    main()

Solution (Python):
t = int(input())
dp = [[0 for i in range(3)] for j in range(t)]
for i in range(t):
    var = list(map(int, input().split()))
    for j in range(3):
        if i == 0:
            dp[i][j] = var[j]
        else:
            for k in range(3):
                if j != k:
                    dp[i][j] = max(dp[i][j], dp[i - 1][k] + var[j])
print(max(dp[-1]))

Solution (Python):
t = int(input())
dp = [[0 for i in range(3)] for j in range(t)]
for i in range(t):
    var = list(map(int, input().split()))
    for j in range(3):
        if i == 0:
            dp[i][j] = var[j]
        else:
            for k in range(3):
                if j != k:
                    dp[i][j] = max(dp[i][j], dp[i - 1][k] + var[j])
print(max(dp[-1]))

Solution (Python):
n=int(input())
lst=[]
for i in range(n):
    lst.append([int(i) for i in input().split()])
dp=[[0,0,0] for i in range(n)]
dp[0]=lst[0]

for i in range(1,n):
    for j in range(3):
        dp[i][j]=lst[i][j]+max(dp[i-1][k] for k in range(3) if k!=j)
print(max(dp[n-1]))

Solution (Python):
N = int(input())
data = [[int(i) for i in input().split()] for j in range(N)]

dp = [[0,0,0] for i in range(N)]
dp[0] = data[0]


for i in range(1,N):
    for j in range(3):
        dp[i][j] = max(dp[i-1][k] for k in range(3) if j!=k) + data[i][j]

        
print(max(dp[-1]))

Solution (Python):
import numpy as np
n=int(input())
ABC=[list(map(int,input().split())) for _ in range(n)]

dp=np.zeros((n,3),int)
for i in range(3):
  dp[0,i]=ABC[0][i]
for i in range(1,n):
  for j in range(3):
    dp[i,j]=max(dp[i-1,(j+1)%3]+ABC[i][j],dp[i-1,(j+2)%3]+ABC[i][j])
print(max(dp[-1]))

Solution (Python):
N = int(input())
X = [list(map(int,input().split())) for i in range(N)]
for i in range(1,N):
    X[i][0]+=max(X[i-1][1],X[i-1][2])
    X[i][1]+=max(X[i-1][0],X[i-1][2])
    X[i][2]+=max(X[i-1][0],X[i-1][1])
print(max(X[N-1]))

Solution (Python):
import sys
n = int(input())
abc_list = []

for i in range(n):
    abc_list.append(list(map(int,input().split())))

pre_suma,pre_sumb,pre_sumc = abc_list[0][0],abc_list[0][1],abc_list[0][2]

if n == 1:
    print(max(pre_suma,pre_sumb,pre_sumc))
    sys.exit()

for i in range(1,n):
    a,b,c = abc_list[i][0],abc_list[i][1],abc_list[i][2]
    suma = a + max(pre_sumb,pre_sumc)
    sumb = b + max(pre_suma,pre_sumc)
    sumc = c + max(pre_suma,pre_sumb)
    pre_suma,pre_sumb,pre_sumc = suma,sumb,sumc
    
print(max(suma,sumb,sumc))

Solution (Python):
if __name__ == '__main__':

	n = int(input())
	res = [[0 for i in range(0, 3)] for i in range(0, n+1)]

	for i in range(1,n+1):
		x, y, z =  map(int,input().split())
		res[i][0] = max(res[i-1][1], res[i-1][2])
		res[i][1] = max(res[i-1][0], res[i-1][2])
		res[i][2] = max(res[i-1][0], res[i-1][1])
		res[i][0]+=x
		res[i][1]+=y
		res[i][2]+=z

	r = max(res[-1])
	print(r)

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = []
    B = []
    C = []
    for _ in range(N):
        a, b, c = [int(x) for x in input().split()]
        A.append(a)
        B.append(b)
        C.append(c)
    
    dp = [[0 for i in range(3)] for i in range(N)]
    dp[0][0] = A[0]
    dp[0][1] = B[0]
    dp[0][2] = C[0]
    for i in range(1, N):
        dp[i][0] = A[i] + max(dp[i - 1][1], dp[i - 1][2])
        dp[i][1] = B[i] + max(dp[i - 1][0], dp[i - 1][2])
        dp[i][2] = C[i] + max(dp[i - 1][1], dp[i - 1][0])
    
    print(max(dp[N - 1]))




if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
abc = [list(map(int, input().split())) for _ in range(N)]

dp = [[0]*3 for _ in range(N)]
dp[0][0] = abc[0][0]
dp[0][1] = abc[0][1]
dp[0][2] = abc[0][2]
for n in range(1, N):
    dp[n][0] = max(dp[n-1][1]+abc[n][0], dp[n-1][2]+abc[n][0])
    dp[n][1] = max(dp[n-1][0]+abc[n][1], dp[n-1][2]+abc[n][1])
    dp[n][2] = max(dp[n-1][0]+abc[n][2], dp[n-1][1]+abc[n][2])
print(max(dp[-1]))

Solution (Python):
N=int(input())
a=[0]*N
b=[0]*N
c=[0]*N
for i in range(N):
    a[i],b[i],c[i]=map(int,input().split())
dp=[[-1 for i in range(3)] for j in range(N+1)]
dp[0] = [0]*3
for i in range(0,N):
    dp[i+1][0] = max(dp[i][1],dp[i][2])+a[i]
    dp[i+1][1] = max(dp[i][2],dp[i][0])+b[i]
    dp[i+1][2] = max(dp[i][0],dp[i][1])+c[i]
print(max(dp[N]))

Solution (Python):

N = int(input())
abc = [list(map(int,input().split())) for _ in range(N)]
dp = [[0]*N for _ in range(3)] 
dp[0][0] = abc[0][0]
dp[1][0] = abc[0][1]
dp[2][0] = abc[0][2]

def chmax(a,b):
    if a < b:
        return b
    else:
        return a 

for i in range(N-1):
    dp[0][i+1] =  chmax(dp[1][i]+abc[i+1][0],dp[2][i]+abc[i+1][0])
    dp[1][i+1] =  chmax(dp[0][i]+abc[i+1][1],dp[2][i]+abc[i+1][1])
    dp[2][i+1] =  chmax(dp[1][i]+abc[i+1][2],dp[0][i]+abc[i+1][2])


ans = chmax(dp[0][N-1],dp[1][N-1])
ans = chmax(ans,dp[2][N-1])
    
print(ans)

Solution (Python):
import numpy as np
n=int(input())
ABC=[list(map(int,input().split())) for _ in range(n)]

dp=np.zeros((n,3),int)
for i in range(3):
  dp[0][i]=ABC[0][i]
for i in range(n-1):
  for j in range(3):
    dp[i+1][j]=max(dp[i][(j+1)%3]+ABC[i+1][j], dp[i][(j+2)%3]+ABC[i+1][j])
print(max(dp[-1]))

Solution (Python):
n=int(input())
abc=[list(map(int,input().split())) for _ in range(n)]
dp=[abc[0]]
for i in range(1,n):
    dp.append([])
    dp[-1].append(max(dp[i-1][1]+abc[i][0],dp[i-1][2]+abc[i][0]))
    dp[-1].append(max(dp[i-1][0]+abc[i][1],dp[i-1][2]+abc[i][1]))
    dp[-1].append(max(dp[i-1][0]+abc[i][2],dp[i-1][1]+abc[i][2]))
print(max(dp[-1]))

Solution (Python):
N = int(input())
abc = [list(map(int, input().split())) for i in range(N)]

dp = [[0] * 3] * N
for idx, val in enumerate(abc):
    a, b, c = val
    if idx == 0:
        dp[0] = val
    else:
        tmp = []
        tmp.append(max(dp[idx - 1][1] + a, dp[idx - 1][2] + a))
        tmp.append(max(dp[idx - 1][0] + b, dp[idx - 1][2] + b))
        tmp.append(max(dp[idx - 1][1] + c, dp[idx - 1][0] + c))
        dp[idx] = tmp
print(max(dp[-1]))

Solution (Python):
n=int(input())
abc=[list(map(int,input().split())) for _ in range(n)]
dp=[abc[0]]
for i in range(1,n):
    dp.append([])
    dp[-1].append(max(dp[i-1][1]+abc[i][0],dp[i-1][2]+abc[i][0]))
    dp[-1].append(max(dp[i-1][0]+abc[i][1],dp[i-1][2]+abc[i][1]))
    dp[-1].append(max(dp[i-1][0]+abc[i][2],dp[i-1][1]+abc[i][2]))
print(max(dp[-1]))

Solution (Python):
n=int(input())
a,b,c=map(int,input().split())
dp=[[a,b,c] for i in range(n)]
for i in range(1,n):
    a,b,c=map(int,input().split())
    dp[i][0]=max(dp[i-1][1],dp[i-1][2])+a
    dp[i][1]=max(dp[i-1][2],dp[i-1][0])+b
    dp[i][2]=max(dp[i-1][0],dp[i-1][1])+c
print(max(dp[n-1]))

Solution (Python):
n=int(input())
dp=[0,0,0]
for i in range(n):
    a,b,c=map(int,input().split())
    dp=[max(dp[1],dp[2])+a,max(dp[0],dp[2])+b,max(dp[0],dp[1])+c]
print(max(dp))

Solution (Python):
import sys
N = int(input())
summer = []
for i in range(N):
    summer.append(list(map(int, sys.stdin.readline().rstrip().split(" "))))

INF = float('inf')
DP = [[-INF for i in range(3)] for i in range(N)]

for i in range(N):
    if i == 0:
        DP[i] = summer[i]
    else:
        DP[i][0] = max(DP[i-1][1], DP[i-1][2]) + summer[i][0]
        DP[i][1] = max(DP[i-1][0], DP[i-1][2]) + summer[i][1]
        DP[i][2] = max(DP[i-1][0], DP[i-1][1]) + summer[i][2]

print(max(DP[N-1]))

Solution (Python):
n= int(input())
active = [[0, 0, 0]] * n

for i in range(n):
  a, b, c = map(int, input().split(' '))
  active[i] = [a, b, c]

dp = [[]] * (n + 1)
for i in range(n+1):
  dp[i] = [0, 0, 0]



def chmin(arr, index_a, val):
  if arr[index_a] > val:
    arr[index_a] = val
    return True
  return False

def chmax(arr, index_a, val):
  if arr[index_a] < val:
    arr[index_a] = val
    return True
  return False

ans = 0
pre_active = None
for i in range(n):
  a, b, c = active[i]
  dp_i = i + 1 
  
  
  dp[dp_i][0] = max(dp[i][1] + a, dp[i][2] + a)
  
  dp[dp_i][1] = max(dp[i][0] + b, dp[i][2] + b)
  
  dp[dp_i][2] = max(dp[i][0] + c, dp[i][1] + c)


print(max(dp[n]))

Solution (Python):
N = int(input())
dp = [[0] * 3 for _ in range(N+1)]
A = [list(map(int,input().split())) for _ in range(N)]

for i in range(N):
    for j in range(3):
        for k in range(3):
            if j == k:
                continue
            dp[i+1][k] = max(dp[i+1][k], dp[i][j] + A[i][k])
print(max(dp[N]))

Solution (Python):
N = int(input())
dp = [[0] * 3 for _ in range(N+1)]

for i in range(N):
    A = list(map(int, input().split()))
    for j in range(3):
        for k in range(3):
            if j == k:
                continue
            dp[i+1][j] = max(dp[i+1][j], dp[i][k] + A[k])

print(max(dp[N]))

Solution (Python):

import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')

n = int(input())
a = [[]] * n
for i in range(n):
	a[i] = [int(x) for x in input().split()]

dp = [[]]
for i in range(n):
	if i == 0:
		dp[i] = a[i]
	else:
		dp.append([0,0,0])
		dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + a[i][0]
		dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + a[i][1]
		dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + a[i][2]

print(max(dp[n-1]))

Solution (Python):
n=int(input())
dp=[0,0,0]
for i in range(n):
    a,b,c=map(int,input().split())
    dp=[max(dp[1],dp[2])+a,max(dp[0],dp[2])+b,max(dp[0],dp[1])+c]
print(max(dp))

Solution (Python):
n = int(input())

abc = []
for i in range(n):
  a,b,c = list(map(int,input().split()))
  abc.append((a,b,c))

dp = [[0]*3 for i in range(n)]
dp[0][0] = abc[0][0]
dp[0][1] = abc[0][1]
dp[0][2] = abc[0][2]

for i in range(1,n):
  for j in range(3):
    dp[i][j] = max(dp[i-1][(j+1)%3],dp[i-1][(j+2)%3]) + abc[i][j]
    
    

ans = max(dp[n-1])
print(ans)

Solution (Python):
n=int(input())
a=[[]]
for i in range(n):
    a.append([int(i) for i in input().split()])
dp=[[0,0,0] for i in range(n+1)]
for i in range(1,n+1):
    for j in range(3):
            dp[i][j]=a[i][j]+max(dp[i-1][k] for k in range(3) if k!=j)
print(max(dp[n]))

Solution (Python):




"""
20191118
"""




































































def main():
    N = int(input())
    a = [None for _ in range(N)]
    b = [None for _ in range(N)]
    c = [None for _ in range(N)]
    for i in range(N):
        a[i], b[i], c[i] = map(int, input().split())

    satisfaction = [[0 for _ in range(N)] for _ in range(3)]
    satisfaction[0][0] = a[0]
    satisfaction[1][0] = b[0]
    satisfaction[2][0] = c[0]

    for i in range(1, N):
        satisfaction[0][i] = max(satisfaction[1][i-1], satisfaction[2][i-1]) + a[i]
        satisfaction[1][i] = max(satisfaction[0][i-1], satisfaction[2][i-1]) + b[i]
        satisfaction[2][i] = max(satisfaction[0][i-1], satisfaction[1][i-1]) + c[i]

    return max(satisfaction[0][-1], satisfaction[1][-1], satisfaction[2][-1])


if __name__ == "__main__":
    print(main())

Solution (Python):
n = int(input())
dp = [[0] * 3 for _ in range(n + 1)]
for i in range(1, n + 1):
	a, b, c = map(int, input().split())
	dp[i][0] = max(dp[i][0], dp[i - 1][1] + a, dp[i - 1][2] + a)
	dp[i][1] = max(dp[i][1], dp[i - 1][0] + b, dp[i - 1][2] + b)
	dp[i][2] = max(dp[i][2], dp[i - 1][0] + c, dp[i - 1][1] + c)
print(max(dp[-1]))

Solution (Python):
def chmax(a,b):
    if a > b:
        return a
    else:
        return b

N = int(input())
A = [list(map(int, input().split())) for _ in range(N)] 

dp = [[0, 0, 0] for _ in range(N)]

dp[0][0] = A[0][0]
dp[0][1] = A[0][1]
dp[0][2] = A[0][2]

for i in range(1,N):
    dp[i][0] = chmax(dp[i-1][1]+A[i][0], dp[i-1][2]+A[i][0])
    dp[i][1] = chmax(dp[i-1][0]+A[i][1], dp[i-1][2]+A[i][1])
    dp[i][2] = chmax(dp[i-1][0]+A[i][2], dp[i-1][1]+A[i][2])

print(max(dp[N-1][0], dp[N-1][1], dp[N-1][2]))

Solution (Python):



from sys import stdin
input = stdin.readline






def chmax(a, b):
    """
    引数２つの値のうち、大きい値を返す

    Args:
        a (int): 比較対象１
        b (int): 比較対象２

    Returns:
        (int): a,bのうち大きい値
    """
    if a < b:
        a = b
    return a





N = int(input())
abc = [list(map(int, input().split())) for _ in range(N)]


dp = [[0] * 3 for _ in range(N)]
for i in range(3):
    dp[0][i] = abc[0][i]

for now_day in range(1, N):
    for select_yesterday in range(3):
        for select_today in range(3):
            if select_today == select_yesterday:
                continue
            dp[now_day][select_today] = chmax(dp[now_day][select_today], dp[now_day - 1][select_yesterday] + abc[now_day][select_today])

print(max(dp[N-1]))

Solution (Python):
N = int(input())
abc = []
for _ in range(N):
    a, b, c = map(int, input().split())
    abc.append((a, b, c))
dp = [[0]*3 for _ in range(N)]
for i in range(3):
    dp[0][i] = abc[0][i]
for i in range(1, N):
    a, b, c = abc[i]
    dp[i][0] = max(dp[i-1][1], dp[i-1][2])+a
    dp[i][1] = max(dp[i-1][0], dp[i-1][2])+b
    dp[i][2] = max(dp[i-1][0], dp[i-1][1])+c
print(max(dp[-1][i] for i in range(3)))

Solution (Python):
N = int(input())
DP = [[0,0,0] for _ in range(N+1)] 
Data = []
Data.append([0,0,0])
for _ in range(N):
    Data.append(list(map(int,input().split())))

DP[1][0] = Data[1][0]
DP[1][1] = Data[1][1]
DP[1][2] = Data[1][2]

for i in range(2,N+1):
    DP[i][0] = max(DP[i-1][1],DP[i-1][2])+Data[i][0]
    DP[i][1] = max(DP[i-1][2],DP[i-1][0])+Data[i][1]
    DP[i][2] = max(DP[i-1][0],DP[i-1][1])+Data[i][2]

ans = max(DP[N][0],DP[N][1],DP[N][2])
print(ans)

Solution (Python):
n = int(input())
dp = [[0, 0, 0] for i in range(n+1)]
for i in range(1, n+1) :
    a,b,c = map(int,input().split())
    dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + a
    dp[i][1] = max(dp[i-1][2], dp[i-1][0]) + b
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + c

print(max(dp[i]))

Solution (Python):
def main():
    N = int(input())

    dp = [0] * 3
    n_dp = [0] * 3

    for _ in range(N):
        a = list(map(int, input().split()))
        for j in range(3):
            n_dp[j] = max(dp[j-2] + a[j], dp[j-1] + a[j])
        dp = n_dp.copy()

    print(max(dp))


main()

Solution (Python):
n=int(input())
abc = [[int(i) for i in input().split()] for i in range(n)]

dp = [[0]*3]*n

dp[0]= abc[0]

for i in range(1,n):
	dp[i] = [abc[i][0]+max(dp[i-1][1], dp[i-1][2]),
			abc[i][1]+max(dp[i-1][0], dp[i-1][2]),
			abc[i][2]+max(dp[i-1][0], dp[i-1][1])]
print(max(dp[n-1]))

Solution (Python):
N = int(input())
a = [list(map(int, input().split())) for _ in range(N)]

table = [a[0]]
def dp(n, i):
  for j in range(len(table), n+1):
    table.append([0]*3)
    for k in range(3):
      h = [table[j-1][l]+a[j][k] for l in range(3) if k != l]
      table[j][k] = max(h)
  return table[n][i]

print(max([dp(N-1, i) for i in range(3)]))

Solution (Python):
if __name__ == "__main__":
    N = int(input())
    abc=[]
    for i in range(N):
        abc.append(list(map(int, input().split())))

    dp=[[abc[0][0],abc[0][1],abc[0][2]]]
    for i in range(N-1):
        dp.append([-1,-1,-1])

    for i in range(1,N):
        dp[i][0]=abc[i][0]+max(dp[i-1][1],dp[i-1][2])
        dp[i][1]=abc[i][1]+max(dp[i-1][2],dp[i-1][0])
        dp[i][2]=abc[i][2]+max(dp[i-1][0],dp[i-1][1])
        None
    
    print(max(
        dp[-1][0],
        dp[-1][1],
        dp[-1][2]
    ))

Solution (Python):
N = int(input())
ABC = [list(map(int, input().split())) for _ in range(N)]



dp = [[0]*3 for _ in range(N+1)]

for i in range(N):
    for j in range(3):
        for k in range(3):
            if j == k:  
                continue
            dp[i+1][k] = max(dp[i+1][k], dp[i][j]+ABC[i][k])

print(max(dp[N][0], dp[N][1], dp[N][2]))

Solution (Python):
n=int(input())
a=[0]*n
b=[0]*n
c=[0]*n
for i in range(n):
    a[i],b[i],c[i]=list(map(int,input().split()))
dpa=[100000000]*n
dpb=[100000000]*n
dpc=[100000000]*n
dpa[0]=a[0]
dpb[0]=b[0]
dpc[0]=c[0]
for i in range(n-1):
    dpa[i+1]=max(dpb[i]+a[i+1],dpc[i]+a[i+1])
    dpb[i+1]=max(dpa[i]+b[i+1],dpc[i]+b[i+1])
    dpc[i+1]=max(dpa[i]+c[i+1],dpb[i]+c[i+1])


print(max(dpa[n-1],dpb[n-1],dpc[n-1]))

Solution (Python):
n=int(input())
a=[0]*n
b=[0]*n
c=[0]*n
for i in range(n):
    a[i],b[i],c[i]=map(int, input().split())
dpa=[0 for i in range(n+1)]
dpb=[0 for i in range(n+1)]
dpc=[0 for i in range(n+1)]

for i in range(n):
    dpa[i+1]=max(dpb[i]+a[i],dpc[i]+a[i])
    dpb[i+1]=max(dpa[i]+b[i],dpc[i]+b[i])
    dpc[i+1]=max(dpa[i]+c[i],dpb[i]+c[i])

print(max(dpa[n],dpb[n],dpc[n]))

Solution (Python):
n=int(input())
a,b,c=map(int,input().split())
dp=[[a,b,c] for i in range(n)]
for i in range(1,n):
  a,b,c=map(int,input().split())
  dp[i][0]=max(dp[i-1][1],dp[i-1][2])+a
  dp[i][1]=max(dp[i-1][0],dp[i-1][2])+b
  dp[i][2]=max(dp[i-1][0],dp[i-1][1])+c
print(max(dp[n-1]))

Solution (Python):
dp, n = [0, 0, 0], int(input())
for i in range(n):
  a, b, c = map(int, input().split ())
  dp = [max(dp[1], dp[2]) + a, max(dp[0], dp[2]) + b, max(dp[1], dp[0]) + c]
print(max(dp))

Solution (Python):
dp, n = [0, 0, 0], int(input())
for i in range(n):
  a, b, c = map(int, input().split ())
  dp = [max(dp[1], dp[2]) + a,
        max(dp[0], dp[2]) + b,
        max(dp[1], dp[0]) + c]
print(max(dp))

Solution (Python):
n = int(input())
dp = [0, 0, 0]
for i in range(n):
  a, b, c = map(int, input().split ())
  nxt = [max(dp[1], dp[2]) + a,
         max(dp[0], dp[2]) + b,
         max(dp[1], dp[0]) + c
         ]
  dp = nxt
print(max(dp))

Solution (Python):
import numpy as np
n=int(input())
ABC=[list(map(int,input().split())) for _ in range(n)]

dp=np.zeros((n,3),int)
for i in range(n):
  for j in range(3):
    dp[i][j]=ABC[0][j] if i==0 else max(dp[i-1][(j+1)%3]+ABC[i][j], dp[i-1][(j+2)%3]+ABC[i][j])
print(max(dp[-1]))

Solution (Python):
n=int(input())
a=[0]*n
b=[0]*n
c=[0]*n
for i in range(n):
    a[i],b[i],c[i]=map(int, input().split())
dpa=[0 for i in range(n+1)]
dpb=[0 for i in range(n+1)]
dpc=[0 for i in range(n+1)]

for i in range(n):
    dpa[i+1]=max(dpb[i]+a[i],dpc[i]+a[i])
    dpb[i+1]=max(dpa[i]+b[i],dpc[i]+b[i])
    dpc[i+1]=max(dpa[i]+c[i],dpb[i]+c[i])

print(max(dpa[n],dpb[n],dpc[n]))

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    dp = [[0] * 3 for _ in range(n)]
    for i in range(n):
        a, b, c = list(map(int, readline().split()))
        if i == 0:
            dp[i][0] = a
            dp[i][1] = b
            dp[i][2] = c
        else:
            dp[i][0] = max(dp[i-1][1] + a, dp[i-1][2] + a)
            dp[i][1] = max(dp[i-1][0] + b, dp[i-1][2] + b)
            dp[i][2] = max(dp[i-1][0] + c, dp[i-1][1] + c)
    print(max(dp[-1]))


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
n = int(sys.stdin.readline())

L = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
dp = [[0 for i in range(3)] for j in range(n)]
for col in range(3):
    dp[0][col] = L[0][col]


for i in range(1, n):
    dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + L[i][0]
    dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + L[i][1]
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + L[i][2]

print(max(dp[-1]))

Solution (Python):
n = int(input())
a,b,c = 0,0,0
for _ in range(n):
    x, y, z = map(int, input().split())
    a,b,c = max(c,b)+x,max(a,c)+y,max(a,b)+z

print(max(a,b,c))

Solution (Python):
dp = [[0] * 3 for _ in range(10 ** 5 + 2)]

N = int(input())
for i in range(1, N + 1):
    inp = [int(_) for _ in input().split()]
    for j in range(3):
        dp[i][j] = max(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3]) + inp[j]

print(max(dp[N]))

Solution (Python):
import sys
input=sys.stdin.readline

n=int(input())
dp=[[0]*3  for _ in range(n)]
a,b,c=map(int,input().split())
dp[0]=[a,b,c]
for i in range(1,n):
    a,b,c=map(int,input().split())
    dp[i][0]=max(dp[i-1][1],dp[i-1][2])+a
    dp[i][1]=max(dp[i-1][0],dp[i-1][2])+b
    dp[i][2]=max(dp[i-1][0],dp[i-1][1])+c
print(max(dp[n-1]))

Solution (Python):
import numpy as np
N=int(input())
ABC=[]
for i in range(N):
    ABC.append(list(map(int,input().split())))
dp=[[0,0,0] for i in range(N)]
dp[0]=ABC[0]
for i in range(1,N):
    for j in range(3):
        dp[i][j]=max(dp[i-1][(j+1)%3],dp[i-1][(j+2)%3])+ABC[i][j]
print(max(dp[N-1]))

Solution (Python):
n=int(input())
a,b,c=map(int,input().split())
dp=[[a,b,c] for i in range(n)]
for i in range(1,n):
  a,b,c=map(int,input().split())
  dp[i][0]=max(dp[i-1][1],dp[i-1][2])+a
  dp[i][1]=max(dp[i-1][0],dp[i-1][2])+b
  dp[i][2]=max(dp[i-1][0],dp[i-1][1])+c
print(max(dp[n-1]))

Solution (Python):
n=int(input())
a,b,c=map(int,input().split())
dp=[[a,b,c] for i in range(n)]
for i in range(1,n):
  a,b,c=map(int,input().split())
  dp[i][0]=max(dp[i-1][1],dp[i-1][2])+a
  dp[i][1]=max(dp[i-1][0],dp[i-1][2])+b
  dp[i][2]=max(dp[i-1][0],dp[i-1][1])+c
print(max(dp[n-1]))

Solution (Python):
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    dp = [[0] * 3 for _ in range(n)]
    for i in range(n):
        a, b, c = list(map(int, input().rstrip('\n').split()))
        if i == 0:
            dp[i][0] = a
            dp[i][1] = b
            dp[i][2] = c
        else:
            dp[i][0] = max(dp[i-1][1] + a, dp[i-1][2] + a)
            dp[i][0] = max(dp[i-1][1] + a, dp[i-1][2] + a)

            dp[i][1] = max(dp[i-1][0] + b, dp[i-1][2] + b)
            dp[i][1] = max(dp[i-1][0] + b, dp[i-1][2] + b)

            dp[i][2] = max(dp[i-1][0] + c, dp[i-1][1] + c)
            dp[i][2] = max(dp[i-1][0] + c, dp[i-1][1] + c)
    print(max(dp[-1]))


if __name__ == '__main__':
    solve()

Solution (Python):
def main():
    n=int(input())
    a=[0 for _ in range(n)]
    b=[0 for _ in range(n)]
    c=[0 for _ in range(n)]
    for i in range(n):
        a[i],b[i],c[i] = map(int,input().split()) 
    dp=[[0 for _ in range(3)] for _ in range(n+1)]
    for i in range(n):
        dp[i+1][0]=max(dp[i][1]+b[i],dp[i][2]+c[i])
        dp[i+1][1]=max(dp[i][0]+a[i],dp[i][2]+c[i])
        dp[i+1][2]=max(dp[i][0]+a[i],dp[i][1]+b[i])
    print(max(dp[n][0],dp[n][1],dp[n][2]))
    

if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
n=int(input())
ABC=[list(map(int,input().split())) for _ in range(n)]

dp=np.zeros((n,3),int)
for i in range(n):
  for j in range(3):
    dp[i][j]=ABC[0][j] if i==0 else max(dp[i-1][(j+1)%3]+ABC[i][j], dp[i-1][(j+2)%3]+ABC[i][j])
  
print(max(dp[-1]))

Solution (Python):
n=int(input())
ABC=[list(map(int,input().split())) for _ in range(n)]

x,y,z=0,0,0
for a,b,c in ABC:
  x,y,z=max(y,z)+a,max(x,z)+b,max(x,y)+c
print(max(x,y,z))

Solution (Python):
n=int(input())
a=[0]*n
b=[0]*n
c=[0]*n
for i in range(n):
    a[i],b[i],c[i]=list(map(int,input().split()))
dpa=[100000000]*n
dpb=[100000000]*n
dpc=[100000000]*n
dpa[0]=a[0]
dpb[0]=b[0]
dpc[0]=c[0]
for i in range(n-1):
    dpa[i+1]=max(dpb[i]+a[i+1],dpc[i]+a[i+1])
    dpb[i+1]=max(dpa[i]+b[i+1],dpc[i]+b[i+1])
    dpc[i+1]=max(dpa[i]+c[i+1],dpb[i]+c[i+1])


print(max(dpa[n-1],dpb[n-1],dpc[n-1]))

Solution (Python):
n=int(input())
a=[0]*n
b=[0]*n
c=[0]*n
for i in range(n):
    a[i],b[i],c[i]=list(map(int,input().split()))
dpa=[100000000]*n
dpb=[100000000]*n
dpc=[100000000]*n
dpa[0]=a[0]
dpb[0]=b[0]
dpc[0]=c[0]
for i in range(n-1):
    dpa[i+1]=max(dpb[i]+a[i+1],dpc[i]+a[i+1])
    dpb[i+1]=max(dpa[i]+b[i+1],dpc[i]+b[i+1])
    dpc[i+1]=max(dpa[i]+c[i+1],dpb[i]+c[i+1])


print(max(dpa[n-1],dpb[n-1],dpc[n-1]))

Solution (Python):
import numpy as np
N = int(input())
L = [list(map(int, input().split())) for i in range(N)]
dp = np.zeros((N, 3), dtype=int)

for i in range(N):
    dp[i][0] = L[0][0] if i == 0 else max(dp[i-1][1]+L[i][0], dp[i-1][2]+L[i][0])
    dp[i][1] = L[0][1] if i == 0 else max(dp[i-1][0]+L[i][1], dp[i-1][2]+L[i][1])
    dp[i][2] = L[0][2] if i == 0 else max(dp[i-1][0]+L[i][2], dp[i-1][1]+L[i][2])

print(max(dp[-1]))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
ABC = zip(m,m,m)

x,y,z = 0,0,0 
for a,b,c in ABC:
    x,y,z = max(y,z)+a,max(x,z)+b,max(x,y)+c

answer = max(x,y,z)
print(answer)

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    a.append([0,0,0])
    a[i][0],a[i][1],a[i][2]=map(int,input().split())
b=[a[0][0],a[0][1],a[0][2]]
for i in range(1,n):
    b1=b[:]
    b[0]=max(a[i][0]+b1[1],a[i][0]+b1[2])
    b[1]=max(a[i][1]+b1[0],a[i][1]+b1[2])
    b[2]=max(a[i][2]+b1[1],a[i][2]+b1[0])
print(max(b))

Solution (Python):
n=int(input())
a=[]
for i in range(n):
    a.append([0,0,0])
    a[i][0],a[i][1],a[i][2]=map(int,input().split())
a.reverse()
b=[a[0][0],a[0][1],a[0][2]]
for i in range(1,n):
    b1=b[:]
    b[0]=max(a[i][0]+b1[1],a[i][0]+b1[2])
    b[1]=max(a[i][1]+b1[0],a[i][1]+b1[2])
    b[2]=max(a[i][2]+b1[1],a[i][2]+b1[0])
print(max(b))

Solution (Python):
n=int(input())
a=[0]*n
b=a.copy()
c=a.copy()
for i in range(n):
    a[i],b[i],c[i]=map(lambda x: int(x), input().split())

INF=1<<60
dpa=[INF]*(10**5+10)
dpb=dpa.copy()
dpc=dpa.copy()

dpa[0]=a[0]
dpb[0]=b[0]
dpc[0]=c[0]
for i in range(n-1):
    dpa[i+1]=max(a[i+1]+dpb[i], a[i+1]+dpc[i])
    dpb[i+1]=max(b[i+1]+dpa[i], b[i+1]+dpc[i])
    dpc[i+1]=max(c[i+1]+dpa[i], c[i+1]+dpb[i])
print(max(dpa[n-1],dpb[n-1],dpc[n-1]))

Solution (Python):
N = int(input())
days = [list(map(int, input().split())) for i in range(N)]
scores = [[0,0,0] for i in range(N)]
scores[0] = days[0]

for i in range(N-1):
    for j in range(3):
        for k in range(3):
            if j==k:
                continue
            newscore = days[i+1][j] + scores[i][k]
            if newscore > scores[i+1][j]:
                scores[i+1][j] = newscore

print(max(scores[-1]))

Solution (Python):
n = int(input())
l = [[0, 0, 0] for _ in range(n)]
for i in range(n):
    at, bt, ct = list(map(int, input().split()))
    l[i] = [at, bt, ct]
m = [[0,0,0] for _ in range(n)]
m[0] = l[0]
for i in range(1,n):
    m[i][0] = l[i][0] + max(m[i-1][1], m[i-1][2])
    m[i][1] = l[i][1] + max(m[i-1][0], m[i-1][2])
    m[i][2] = l[i][2] + max(m[i-1][0], m[i-1][1])
print(max(m[-1]))

Solution (Python):
def main():
    n = int(input())
    abc = []
    for i in range(n):
        abc.append(tuple(map(int, input().split())))

    am, bm, cm = abc[0]
    for ai, bi, ci in abc[1:]:
        am, bm, cm = max(bm, cm) + ai, max(am, cm) + bi, max(am, bm) + ci
    print(max(am, bm, cm))


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
a = [[0]*3 for i in range(N)]

for i in range(N):
  a[i] = [int(i) for i in input().split()] 

def summerVacation(n, i):
  if n ==0:
    dp[n][i] = a[n][i]
    return
  i1 = (i+1) % 3
  i2 = (i+2) % 3  
  dp[n][i] = max( dp[n-1][i1], dp[n-1][i2] ) + a[n][i]  
  
dp =[[None]*3 for _ in range(N)]
for n in range(N):
  for i in range(3):
    summerVacation(n, i)
    
print( max( dp[N-1][0], dp[N-1][1], dp[N-1][2] ))

Solution (Python):
N = int(input())
ABC = [[int(x) for x in input().split()] for _ in range(N)]
dp = [[-1, -1, -1] for _ in range(N)]
dp[0] = ABC[0]
i = 1
for a, b, c in ABC[1:]:
    dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + a 
    dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + b 
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + c 
    i += 1
print(max(dp[N-1]))

Solution (Python):
import sys
sys.setrecursionlimit(1000000)

N = int(input())
a = [[0]*3 for i in range(N)]

for i in range(N):
  a[i] = [int(i) for i in input().split()] 

def f(n, i):
  if n ==0:
    return a[0][i]
  i1 = (i+1) % 3
  i2 = (i+2) % 3
  if F[n][i]:
    return F[n][i]
  F[n][i] = max(f(n-1, i1), f(n-1, i2)) + a[n][i] 
  return F[n][i]  

F =[[None]*3 for _ in range(N)]

print(max(f(N-1, 0), f(N-1,1), f(N-1,2)))

Solution (Python):
n = int(raw_input())

dp = [0]*(3)

for i in range(n):
    dp_new = [0]*3
    c=map(int,raw_input().split())
    
    for i in range(3):
        for j in range(3):
            if i != j:
                dp_new[j] = max(dp_new[j],dp[i]+c[j])
    dp = dp_new
print(max(dp))

Solution (Python):
import numpy as np
n = int(input())
ans = 0
field = [map(int,input().split()) for _ in range(n)]
x,y,z = 0,0,0


for a,b,c in field:
  
  x,y,z = max(y,z) + a,max(x,z) + b,max(x,y) + c

print(max(x,y,z))

Solution (Python):
n = int(input())
h = [list(map(int, input().split())) for _ in range(n)]

dp = [[0] * 3 for _ in range(n + 1)]
for i in range(1, n + 1):
    for j in range(3):
        for k in range(3):
            
            if j == k:
                continue
            dp[i][k] = max(dp[i][k], dp[i - 1][j] + h[i - 1][k])
print(max(dp[n]))

Solution (Python):
def resolve():
    N = int(input())

    A = [0 for i in range(N+1)]
    B = [0 for i in range(N+1)]
    C = [0 for i in range(N+1)]

    for i in range(1,len(A)):
        a,b,c = map(int,input().split())

        A[i] = max(B[i-1],C[i-1]) + a
        B[i] = max(C[i-1],A[i-1]) + b
        C[i] = max(A[i-1],B[i-1]) + c

    print(max(A[-1],B[-1],C[-1]))
    
resolve()

Solution (Python):
n=int(input())
it=[[],[],[]]
for i in range(n):
    aa=list(map(int,input().split()))
    it[0].append(aa[0])
    it[1].append(aa[1])
    it[2].append(aa[2])
dp=[[0,0,0] for i in range(n)]
for i in range(3):
    dp[0][i]=it[i][0]
for i in range(1,n):
    for j in range(3):
        for k in range(3):
            if k==j:
                continue
            dp[i][j]=max(dp[i][j],dp[i-1][k]+it[j][i])

print(max(dp[-1]))

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
n=int(input())
l=[]
for i in range(n):
  l.append(list(map(int,input().split())))
dp=[[0,0,0] for i in range(n)]
dp[0]=l[0]
def dp1(x):
  if dp[x]!=[0,0,0]:
    return dp[x]
  else:
    dp2=dp1(x-1)
    ansa=l[x][0]+max(dp2[1],dp2[2])
    ansb=l[x][1]+max(dp2[0],dp2[2])
    ansc=l[x][2]+max(dp2[1],dp2[0])
    dp[x]=[ansa,ansb,ansc]
    return dp[x]
print(max(dp1(n-1)))